You are given a binary string of length nn. You have exactly kk moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped (00 becomes 11, 11 becomes 00). You need to output the lexicographically largest string that you can get after using all kk moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.
A binary string aa is lexicographically larger than a binary string bb of the same length, if and only if the following holds:
- in the first position where aa and bb differ, the string aa contains a 11, and the string bb contains a 00.
Input
The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.
Each test case has two lines. The first line has two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105; 0≤k≤1090≤k≤109).
The second line has a binary string of length nn, each character is either 00 or 11.
The sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, output two lines.
The first line should contain the lexicographically largest string you can obtain.
The second line should contain nn integers f1,f2,…,fnf1,f2,…,fn, where fifi is the number of times the ii-th bit is selected. The sum of all the integers must be equal to kk.
Example
input
Copy
6 6 3 100001 6 4 100011 6 0 000000 6 1 111001 6 11 101100 6 12 001110
output
Copy
111110 1 0 0 2 0 0 111110 0 1 1 1 0 1 000000 0 0 0 0 0 0 100110 1 0 0 0 0 0 111111 1 2 1 3 0 4 111110 1 1 4 2 0 4
思路:可以发现如果k是偶数的话所有的数都不用变,1还是1,0还是0,所以我们遇到0的时候需要消耗一次不变次数,让他的操作数转化成单数,然后就可以把0变成1了,当前面的都最优转换的时候,如果k还有剩余,那么就放在最后一个字符上,然后再算一下操作步数,如果是奇数那么就变,如果是偶数那么就不变。因此我们列举的时候遍历的是0~n-2,s[n-1]单独去讨论。当操作数k是奇数的话,1变成0,0变成1,所以我们需要阻止1变成0,那么我们就在遇到1的时候消耗一个操作次数,让总共操作数变成偶数,这样就能保持1还是1,当我们用完消耗次数的时候,以后的1就都得转换成0了。
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=2e5+7;
ll n,k;
ll a[N];
void sove(){
string s;
cin>>n>>k;
cin>>s;
memset(a,0,sizeof a);
ll con=k;
if(k==0){
cout<<s<<endl;
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
return ;
}
if(con%2!=0){
for(int i=0;i<n-1;i++){
if(s[i]=='0'){
s[i]='1';
}else{
if(k>0){
a[i]=1;
k--;
}else{
s[i]='0';
}
}
}
if(k>0){
a[n-1]=k;
if((con-k)%2!=0){
if(s[n-1]=='0')s[n-1]='1';
else s[n-1]='0';
}
}else {
if(s[n-1]=='0')s[n-1]='1';
else s[n-1]='0';
}
}else{
for(int i=0;i<n-1;i++){
if(s[i]=='0'&&k>0){
k--;
s[i]='1';
a[i]=1;
}
}
if(k>0){
a[n-1]=k;
if((con-k)%2!=0){
if(s[n-1]=='0')s[n-1]='1';
else s[n-1]='0';
}
}
}
cout<<s<<endl;
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie() ,cout.tie() ;
int t;
cin>>t;
while(t--){
sove();
}
return 0;
}