AcWing 778. 字符串最大跨距
原题链接
困难
作者:
yoruichi
,
2021-03-04 09:16:31
,
所有人可见
,
阅读 186
C++ 代码
#include <iostream>
using namespace std;
int main()
{
string s,s1,s2;
int n,m,loc1=-1,loc2=-1;
char c;
while(cin>>c,c!=',') s+=c;
while(cin>>c,c!=',') s1+=c;
while(cin>>c) s2+=c;
n = s1.size();
m = s2.size();
for(int i=0;i<s.size();i++)
{
string sub = s.substr(i,n);
if(sub == s1)
{
loc1 = i;
break;
}
}
//cout << loc1<<endl;
for(int j=s.size()-m;j>0;j--)
{
string sub = s.substr(j,m);
if(sub == s2)
{
loc2 = j;
break;
}
}
//cout << loc2<<endl;
int ans = loc2-loc1-n;
if(loc1 == -1 || loc2 == -1 || ans < 0 ) cout << -1 <<endl;
else cout << ans <<endl;
return 0;
}