【B.寻寻觅觅寻不到】暴力,对于两个字符串a、b,如果a[pos]!=b[pos],那么截取的k一定在pos处或者左边,枚举k就行了。特判有点麻烦。
#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
using namespace std;
const int N = 3e5 + 10;
string s, t;
int n1, n2, k;
int main() {
int T;
cin >> T;
while (T--) {
cin >> s >> t >> k;
n1 = sz(s), n2 = sz(t);
if (n1 != n2) {
cout << "NO\n";
continue;
}
if (k > n1) {
cout << "NO\n";
continue;
}
if (s == t) {
cout << "YES\n";
continue;
}
int pos = 0;
for (; pos < n1; pos++) {
if (s[pos] != t[pos]) break;
}
// 遍历所有的情况
bool res = false;
for (int i = 0; i <= k; i++) {
int post = pos - i;
if (post < 0) break;
if (post + k > n1) {
continue;
}
string tmp1 = s.substr(post, k);
string tmp = s.substr(0, post) + s.substr(post + k) + tmp1;
if (tmp == t) res = true;
}
if (res)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}

京公网安备 11010502036488号