sgu 546. Ternary Password 模拟

本文介绍了一个关于Toogle网站密码设置的问题。为了符合该网站的安全政策,密码必须包含特定数量的'0'和'1'字符,其余为'2'。文章提供了一段代码,用于计算并展示将现有密码调整到符合要求所需的最小字符替换次数及调整后的密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

546. Ternary Password
Time limit per test: 1 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



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 exactly a 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 na 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.

Example(s)
sample input
sample output
6 1 3
012022
2
111022

sample input
sample output
5 5 0
02211
4
00000



#include <bits/stdc++.h>
using namespace std;

char c[205];
int num[3];
int main()
{
    int chan[3];
    int n;
    scanf("%d%d%d%s",&n,&chan[0],&chan[1],&c);
    chan[2]=n-chan[0]-chan[1];
    if(chan[0]+chan[1]>n){
        printf("-1");
        return 0;
    }

    for(int i=0;i<n;i++){
        if(c[i]=='0')num[0]++;
        if(c[i]=='1')num[1]++;
    }
    num[2]=n-num[0]-num[1];

    int ans=0;
    for(int i=0;i<n;i++){
        if(num[c[i]-'0']>chan[c[i]-'0']){
            for(int j=0;j<=2;j++){
                if(num[j]<chan[j]){
                    num[c[i]-'0']--;
                    num[j]++;
                    c[i]='0'+j;
                    ans++;
                    break;
                }
            }
        }
    }
    printf("%d\n%s",ans,c);


    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值