题解:P15498 [ICPC 2025 APC] Corrupted File
容易发现一次操作等价于删除一个
我们可以贪心地对两个字符串进行匹配,有以下三种情况:
- 如果当前匹配到的两个字符相同就匹配成功。
- 如果在
B 中为1 ,C 中为0 ,则让B 的字符继续前进直到匹配成功。 - 如果在
B 中为0 且未匹配成功,说明B 中这个0 和上一个0 之间的1 的数量比C 中少,无法和C 中的下一个0 进行匹配,只能删除中间所有的1 ,将两个0 合并。如果B 当前是第一个0 就直接失败。
#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;
}