容易发现一次操作等价于删除一个 或者删除连续的两个 中的一个,也就是 是可以随意删除的。
我们可以贪心地对两个字符串进行匹配,有以下三种情况:
- 如果当前匹配到的两个字符相同就匹配成功。
- 如果在 中为 , 中为 ,则让 的字符继续前进直到匹配成功。
- 如果在 中为 且未匹配成功,说明 中这个 和上一个 之间的 的数量比 中少,无法和 中的下一个 进行匹配,只能删除中间所有的 ,将两个 合并。如果 当前是第一个 就直接失败。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
int n, m;
string s, t;
void solve(){
cin >> n >> m >> s >> t;
s = ' ' + s;
t = ' ' + t;
int i = 1, j = 1, lst = 0;
for(; i <= n; i++){
//情况二直接跳过
if(j <= m && s[i] == '1' && t[j] == '1') j++; //情况一
else if(j <= m && s[i] == '0' && t[j] == '0') lst = ++j;//情况一,顺便记录上一个 0 的位置
else if(s[i] == '0'){//情况三,回到上一个 0 的位置
j = lst;
if(!lst) break;
}
}
cout << (j > m ? "yes\n" : "no\n");//C 中所有字符匹配完就是 yes
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
while(T--) solve();
return 0;
}