题目描述
s1,s2在s中最大跨距
(暴力枚举) $O(n^2)(doge)$
直接上暴力美学,一个从左边搜,一个从右边搜,搜到就break,然后输出距离
C++ 代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int f = 0, l = -5, r = -5;
string ss, s = "", s1 = "", s2 = "";
cin >> ss;
for(int i = 0; i < ss.size(); i ++){
if(ss[i] == ','){
f ++;
continue;
}
if(f == 0)s += ss[i];
else if(f == 1)s1 += ss[i];
else s2 += ss[i];
}
for(int i = 0; i < s.size(); i ++){
string s3 = s.substr(i, s1.size());
if(s3 == s1){l = i + s1.size();break;}
}
for(int i = s.size() - 1; i >= 0; i --){
string s4 = s.substr(i - s2.size() + 1, s2.size());
if(s4 == s2){r = i - s2.size();break;}
}
if(l == -5 || r == -5 || r - l <= 0)cout << -1 <<endl;
else cout << r - l + 1 << endl;
return 0;
}