#include<iostream>
#include<string>
using namespace std;
int main(){
int t=0;
cin>>t;
while(t--){
int n=0,k=0;
string a;
int b[200005]={0};
cin>>n>>k;//k奇动1,k偶动0
cin>>a;
//以上为输入
if(k%2){//k奇动1
int j=0;
for(int i=0;i<n;i++){
if(a[i]=='1'&&j<k){
b[i]++;
j++;
}
}
b[n-1]+=k-j;
for(int i=0;i<n;i++){
if(b[i]%2) cout<<a[i];
else cout<<!(a[i]-'0');
}
cout<<endl;
for(int i=0;i<n;i++){
cout<<b[i]<<' ';
}
cout<<endl;
}else{//k偶动0
int j=0;
for(int i=0;i<n;i++){
if(a[i]=='0'&&j<k){
b[i]++;
j++;
}
}
b[n-1]+=k-j;
for(int i=0;i<n;i++){
if(b[i]%2) cout<<!(a[i]-'0');
else cout<<a[i];
}
cout<<endl;
for(int i=0;i<n;i++){
cout<<b[i]<<' ';
}
cout<<endl;
}
}
return 0;
}
啧,真长,优化一下,把奇偶合并
#include<iostream>
#include<string>
using namespace std;
int main(){
int t=0;
cin>>t;
while(t--){
int n=0,k=0;
string a;
int b[200005]={0};
cin>>n>>k;//k奇动1,k偶动20
cin>>a;
int j=0,p=k%2;
for(int i=0;i<n;i++){
if(a[i]-'0'==p&&j<k){
b[i]++;
j++;
}
}
b[n-1]+=k-j;
for(int i=0;i<n;i++){
if(b[i]%2==p) cout<<a[i];
else cout<<!(a[i]-'0');
}
cout<<endl;
for(int i=0;i<n;i++){
cout<<b[i]<<' ';
}
cout<<endl;
}
return 0;
}
|