AcWing 778. 字符串最大跨距
原题链接
困难
作者:
xi.xi
,
2022-12-02 00:52:50
,
所有人可见
,
阅读 127
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string str,a,b,c;
int i=0;
cin>>str;
while(str[i]!=',') a+=str[i++]; i++;
while(str[i]!=',') b+=str[i++]; i++;
while(i<str.size()) c+=str[i++];
int left,right;
left=a.find(b);
right=a.rfind(c);
if(left+b.size()-1<right&&left!=-1&&right!=-1) cout<<right-(left+b.size());
else cout<<"-1";
return 0;
}
//
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string str,a,b,c;
int i=0;
cin>>str;
while(str[i]!=',') a+=str[i++]; i++;
while(str[i]!=',') b+=str[i++]; i++;
while(i<str.size()) c+=str[i++];
int left=0,right=a.size()-c.size();
while(left+b.size()<a.size())
{
int k=0;
while(k<b.size())
{
if(a[k+left]!=b[k]) break;
k++;
}
if(k==b.size()) break;
left++;
}
while(right>=0)
{
int j=0;
while(j<c.size())
{
if(a[right+j]!=c[j]) break;
j++;
}
if(j==c.size()) break;
right--;
}
if(left+b.size()-1<right) cout<<right-left-b.size();
else cout<<-1;
return 0;
}
//