fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. bool isSubsequence(const string& s1, const string& s2, int l, int r) {
  6. int i = 0, j = 0;
  7. while (i < s1.length() && j < s2.length()) {
  8. if (i >= l && i < r && s1[i] == s2[j]) {
  9. j++;
  10. }
  11. i++;
  12. }
  13. return j == s2.length();
  14. }
  15.  
  16. int main() {
  17. int T;
  18. cin >> T;
  19. while (T--) {
  20. int m, n;
  21. cin >> m >> n;
  22. string s1, s2;
  23. cin >> s1 >> s2;
  24.  
  25. bool found = false;
  26. for (int i = 0; i <= m - n; i++) {
  27. if (isSubsequence(s1, s2, i, i + n)) {
  28. found = true;
  29. break;
  30. }
  31. }
  32. cout << (found ? "YES" : "NO") << endl;
  33. }
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 5320KB
stdin
2
4 2
acdc
ad
4 2
accd
ad
stdout
NO
NO