AcWing 778. 字符串最大跨距
原题链接
简单
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string s,a,b;
char c;
while(cin>>c,c!=',') s=s+c;
while(cin>>c,c!=',') a=a+c;
while(cin>>c) b=b+c;
int sa=s.size(),aa=a.size(),ba=b.size();
int s1,s2;
for(int i=0;i+aa<=sa;i++)
{
int j=0,count=0;
while(j<aa&&s[i+j]==a[j])
{
j++;
count++;
}
if(count==aa)
{
s1=i;
break;
}
}
for(int i=sa-ba;i>=0;i--)
{
int j=0,count=0;
while(j<ba&&s[i+j]==b[j])
{
j++;
count++;
}
if(count==ba)
{
s2=i;
break;
}
}
s1+=aa-1;
if(s1>=s2) {
puts("-1");
return 0;
}
int mo=s2-s1-1;
cout<<mo<<endl;
return 0;
}