AcWing 778. 字符串最大跨距
原题链接
简单
作者:
深街酒徒
,
2024-11-28 21:40:18
,
所有人可见
,
阅读 2
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
string s, s1, s2;
char c;
while(cin >> c, c != ',') s += c;
while(cin >> c, c != ',') s1 += c;
cin >> s2;
int len = s.size(), len1 = s1.size(), len2 = s2.size();
int idx = -1;
for(int i = 0; i <= len - len1; i++)
{
int j = i, k = 0;
while(s1[k] == s[j] && s[j]) k++, j++;
if(k == len1)
{
idx = i + k - 1;
break;
}
}
if(idx == -1)
{
cout << "-1";
return 0;
}
int idx2 = -1;
for(int i = 0; i <= len - len2; i++)
{
int j = i, k = 0;
while(s2[k] == s[j] && s[j]) k++, j++;
if(k == len2) idx2 = i;
}
if(idx2 == -1 || idx >= idx2)
{
cout << "-1";
return 0;
}
cout << idx2 - idx - 1;
return 0;
}