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