fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main() {
  6. int T; // Number of test cases
  7. cin >> T;
  8.  
  9. while (T--) {
  10. int n, m;
  11. string s, t;
  12.  
  13. cin >> n >> m; // Lengths of strings s and t
  14. cin >> s >> t; // Strings s and t
  15.  
  16. int i = 0, j = 0;
  17.  
  18. // Use two pointers to check if t is a subsequence of s
  19. while (i < n && j < m) {
  20. if (s[i] == t[j]) {
  21. j++; // Move pointer for t if there's a match
  22. }
  23. i++; // Always move pointer for s
  24. }
  25.  
  26. // If we've matched all characters of t, print YES, otherwise NO
  27. if (j == m) {
  28. cout << "YES" << endl;
  29. } else {
  30. cout << "NO" << endl;
  31. }
  32. }
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0.01s 5292KB
stdin
2
4 2
acdc
ad
4 2
accd
ad
stdout
YES
YES