fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // 判断删除子串后的s是否等于t
  6. bool canBeMadeEqual(string& s, string& t, int l, int r) {
  7. string modified = s.substr(0, l) + s.substr(r); // 删除 s[l, r) 这段子串
  8. return modified == t; // 检查是否与 t 相等
  9. }
  10.  
  11. int main() {
  12. int T;
  13. cin >> T;
  14.  
  15. while (T--) {
  16. int m, n;
  17. cin >> m >> n;
  18. string s, t;
  19. cin >> s >> t;
  20.  
  21. bool found = false;
  22. for (int i = 0; i <= m - n; i++) {
  23. // 尝试删除 s 中的任意一个子串,使得剩余部分与 t 相等
  24. for (int j = i + n; j <= m; j++) {
  25. if (canBeMadeEqual(s, t, i, j)) {
  26. found = true;
  27. break;
  28. }
  29. }
  30. if (found) break;
  31. }
  32.  
  33. cout << (found ? "YES" : "NO") << endl;
  34. }
  35.  
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0.01s 5292KB
stdin
2
4 2
acdc
ad
4 2
accd
ad
stdout
NO
YES