AcWing 778. 字符串最大跨距
原题链接
困难
作者:
NeonSean
,
2020-08-15 16:34:59
,
所有人可见
,
阅读 325
双指针找子字符串位置
#include <iostream>
using namespace std;
int main() {
string s, s1, s2, st;
getline(cin, st);
int l = 0, r;
for (int i = 0; st[i]; i++) {
if (st[i] == ',' && l == 0) l = i;
else if (st[i] == ',' && l != 0) r = i;
}
s = st.substr(0, l);
s1 = st.substr(l + 1, r - l - 1);
s2 = st.substr(r + 1,st.size() - r - 1);
//cout << s << ' ' << s1 << ' ' << s2;
int m = -1, n = -1;
for (int i = 0; i < s.size(); i++ ) {
int j = 0;
int k = i;
while (s[k] == s1[j] && j < s1.size()) {
k++;
j++;
}
if (j == s1.size()){
m = k - 1;
break;
}
}
for (int i = s.size() - 1;i > 0; i--) {
int j = s2.size() - 1;
int k = i;
while (s[k] == s2[j] && j >= 0) {
k--;
j--;
}
if (j == -1) {
n = k + 1;
break;
}
}
if (m == -1 || n == -1 || (n - m < 0)) cout << "-1" << endl;
else cout << n - m - 1 << endl;
return 0;
}