题目描述
字符串最大跨距
样例
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
string s, s1, s2;
char a;
while (cin >> a && a != ',') s += a;
while (cin >> a && a != ',') s1 += a;
while (cin >> a) s2 += a;
if (s1.size() > s.size() || s2.size() > s.size()) cout << -1 << endl;
else {
int l = 0;
while (l + s1.size() < s.size())
{
int k = 0;
while (k<s1.size()&&s[l+k]==s1[k])
k++;
if (k == s1.size()) break;
else l++;
}
int r = s.size() - s2.size();
while (r >= 0)
{
int j = 0;
while (j<s2.size()&&s[r + j] == s2[j])
j++;
if (j == s2.size() ) break;
else r--;
}
if (l + s1.size() > r) cout << -1 << endl;
else cout<<r-l-s1.size()<<endl;
}
system("pause");
return 0;
}
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla