778.字符串最大跨距
作者:
不想考试
,
2024-11-24 20:18:32
,
所有人可见
,
阅读 2
#include<iostream>
using namespace std;
int main()
{
string s, s1, s2;
char c;
while(cin >> c,c != ',') s += c;
while(cin >> c,c != ',') s1 += c;
while(cin >> c) s2 += c;
if(s1.size() > s.size() || s2.size() > s.size()) puts("-1");
else
{
//找到最左边s1的位置
int l = 0;
while(l + s1.size() < s.size())
{
int k = 0;
while(k < s1.size())
{
if(s[l + k] != s1[k]) break;
k ++;
}
if(k == s1.size()) break;//确保最左边s1
l ++;
}
//找到最右边s2的位置
int r = s.size() - s2.size();
while(r >= 0)
{
int k = 0;
while(k < s2.size())
{
if(s[r + k] != s2[k]) break;
k ++;
}
if(k == s2.size()) break;//确保最右边s2
r --;
}
l += s1.size() - 1;//将l的位置置于s1的右边界
if(l > r) puts("-1");//交叉
else cout << r - l - 1 << endl;
}
return 0;
}