fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // 判断是否可以通过删除一个区间得到 t
  6. bool canBeMadeEqual(string& s, string& t) {
  7. int m = s.length(), n = t.length();
  8.  
  9. // 尝试从前和后依次匹配
  10. int i = 0, j = 0;
  11.  
  12. // 从左到右匹配 t
  13. while (i < m && j < n) {
  14. if (s[i] == t[j]) {
  15. j++;
  16. }
  17. i++;
  18. }
  19.  
  20. // 如果 j == n,说明 t 完全匹配到了 s 的一部分
  21. if (j == n) {
  22. return true;
  23. }
  24.  
  25. return false;
  26. }
  27.  
  28. int main() {
  29. int T;
  30. cin >> T;
  31.  
  32. while (T--) {
  33. int m, n;
  34. cin >> m >> n;
  35. string s, t;
  36. cin >> s >> t;
  37.  
  38. if (canBeMadeEqual(s, t)) {
  39. cout << "YES" << endl;
  40. } else {
  41. cout << "NO" << endl;
  42. }
  43. }
  44.  
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 5312KB
stdin
2
4 2
acdc
ad
4 2
accd
ad
stdout
YES
YES