题目链接
并查集模拟就行
写代码要细心
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1000006;
int pre[N];
int finds(int x){
return x == pre[x]?x: pre[x] = finds(pre[x]);
}
void unions(int x, int y){
int xx = finds(x);
int yy = finds(y);
pre[xx] = yy;
}
char s[N];
signed main()
{
int t;
cin >> t;
while(t --){
int n,k;
cin >> n>> k;
scanf("%s", (s + 1));
for(int i = 0 ; i <= n + 1; ++ i) pre[i] = i;
for(int i = 1; i <= n; i ++){
if(s[i] == '1'){
unions(i, i + 1);
}
}
for(int i = 1; i <= n; i ++){
if(s[i] == '0'){
printf("%c", s[i]);
}
else {
int x = finds(i);
if(x == n + 1) {
for(int j = i; j <= n; j ++){
printf("%c", s[j]);
}
break;
}
else {
if(x - i <= k){
k = k - x + i;
printf("0");
s[x] = '1';
unions(x, x + 1);
}
else {
printf("%c", s[i]);
}
}
}
}
cout << endl;
}
}