Description:
In the ternary world all passwords are ternary, that is, they consist only of digits "0", "1" and "2". Terentius is trying to register on a famous internet service site Toogle, but the problem is, according to the security policy of this service the password must contain exactlya characters "0" and exactly b characters "1". All other characters of the password must be "2".
Terentius was fond of his password, he spent much time trying to remember it and now he can type it even with his eyes closed. That's the reason Terentius wants to replace the minimum number of characters in his password so that it meets the strict requirements of the Toogle policy. Terentius wants only to replace (substitute) some characters in password, he doesn't intend to perform other operations with the password.
Help Terentius find the minimum number of replacements and print the corresponding possible variant of the password to Toogle.
Input:
The first line of the input contains three integers n, a and b (1 ≤ n ≤ 200; 0 ≤ a,b ≤ 200) — the length of Terentius's password, the expected number of characters "0" and the expected number of characters "1" in the password to Toogle. The second line contains Terentius's password. All characters in this line are digits "0", "1" or "2".
Output:
In the first output line print t — the minimum number of replacements. In the second line print the possible variant of the Toogle password — the password that satisfies the Toogle safety policy, that differs from Terentius's password in exactly t positions. If there are several passwords, print any of them. Obviously, the length of the printed password must equal n.
It is possible that Terentius's password already meets the Toogle policy. In this case the first line must contain "0", and the second line must contain Terentius's password.
If the solution doesn't exist, that is, if it is impossible to get a password to Toogle if Terentius uses replacements only, then print "-1" in the first line of the output. In this case print empty second line or don't print the second line at all.
Sample Input:
Sample Input:
6 1 3
012022
sample output
2
111022
sample input
5 5 0
02211
sample output
4
00000
这个道题主要是将字符串中当前‘0’和‘1’的个数和给出的字符串的‘0’的‘1’的个数进行分类比较,分出四种情况,分别进行讨论,具体代码如下:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int len,t0,t1,temp;
int i,cas0,cas1,cas;
char str[300];
while(~scanf("%d %d %d",&len,&t0,&t1)){
getchar();
cas0 = 0;
cas1 = 0;
cas = 0;
temp = 0;
for(i = 0;i < len;i++){
scanf("%c",&str[i]);
if(str[i]=='0') cas0++;
if(str[i]=='1') cas1++;
}
if((t0+t1)>len){
printf("-1\n");
}else if(t0 <= cas0&&t1 <= cas1){
for(i = 0; i < len;i++){
if(str[i]=='0'){
if(cas0-t0>0){
str[i]= '2';
cas0--;
cas++;
}
}else if(str[i] =='1'){
if(cas1-t1>0){
str[i]='2';
cas1--;
cas++;
}
}
}
temp = 1;
}else if(t0 <= cas0&&t1 >= cas1){
int sum0 = cas0 - t0;
int sum1 = t1 - cas1;
int k = 0;
int k1 = 0;
if(sum0>=sum1){
for(i = 0;i<len;i++){
if(str[i]=='0'&&(k<sum1)){
str[i]='1';
cas++;
k++;
}else if((str[i]=='0')&&(k == sum1)&&(k1<sum0-sum1)){
str[i]='2';
cas++;
k1++;
}
}
}else{
for(i = 0; i<len;i++){
if(str[i]=='0'&&k<sum0){
str[i] = '1';
k++;
cas++;
}
if(str[i]=='2'&&(k1<sum1-sum0)){
str[i] = '1';
k1++;
cas++;
}
}
}
temp =1;
}else if(t0>=cas0&&t1<=cas1){
int sum0 = t0 - cas0;
int sum1 = cas1 - t1;
int k1 = 0;
int k2 = 0;
int k0 = 0;
if(sum0 <= sum1){
for(i = 0; i < len;i++){
if(str[i]=='1'&&(k1< sum0)){
str[i] = '0';
cas++;
k1++;
}else if(str[i]=='1'&&(k1 == sum0)&&(k2<sum1-sum0)){
str[i] = '2';
cas++;
k2++;
}
}
}else{
for(i = 0; i < len;i++){
if(str[i] == '1'&&k1<sum1){
str[i] = '0';
cas++;
k1++;
}
if(str[i]=='2'&&(k2< sum0-sum1)){
str[i] = '0';
cas++;
k2++;
}
}
}
temp = 1;
}else if(t0>=cas0&&t1>=cas1){
int k0 = 0;
int k1 = 0;
int sum0 = t0-cas0;
int sum1 = t1-cas1;
for(i = 0; i < len;i++){
if(str[i]=='2'&&(k0<sum0)){
str[i] = '0';
k0++;
cas++;
}
if(k0==sum0) break;
}
for(i = 0; i < len;i++){
if(str[i]=='2'&&(k1<sum1)){
str[i] = '1';
k1++;
cas++;
}
if(k1==sum1) break;
}
temp=1;
}
if(temp){
printf("%d\n",cas);
for(i = 0; i<len;i++){
printf("%c",str[i]);
}
printf("\n");
}
}
return 0;
}