AcWing 778. 字符串最大跨距
原题链接
困难
作者:
胜利庄老实人
,
2021-03-13 10:56:35
,
所有人可见
,
阅读 338
#include<iostream>
using namespace std;
int main(){
string s, s1, s2;
char c;
while(cin >> c, c != ',')
s = s + c;
while(cin >> c, c != ',')
s1 = s1 + c;
while(cin >> c)
s2 = s2 + c;
if(s.size() < s1.size() || s.size() < s2.size())
puts("-1");
else{
int l1 = -1,l2 = -1;
int len = s.size(), len1 = s1.size(), len2 = s2.size();
for(int i = 0; i + len1 < len; ++i){
int j = i, k = 0;
while(k < len1 && s1[k] == s[j]){
++j;
++k;
}
if(k == len1){
l1 = j;
break;
}
}
for(int i = 0; i + len2 <= len; ++i){
int j = i, k = 0;
while(k < len2 && s2[k] == s[j]){
++j;
++k;
}
if(k == len2 && l2 < i)
l2 = i;
}
if(l2 - l1 < 0 || l2 < 0 || l1 < 0)
puts("-1");
else
cout << l2 - l1 << endl;
}
return 0;
}