The variant of passwords

3.5

13 votes
Basic Programming, , Algorithms, Greedy algorithm
Problem

Bob has a string password S of length N consists only of digits 0, 1, and 2. The password needs to follow the given requirements:

  1. Password S must contain exactly A characters "0".
  2. Password S must contain exactly B characters "1".
  3. All other characters of the password S must be "2".

Bob wants to replace the minimum number of characters in the password S so that it meets the given requirements. 

Help Bob find the minimum number of replacements and print the corresponding possible variant of the password. If there are multiple passwords possible, print the lexicographically smallest.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains an integer N denoting the length of the password.
  • The second line of each test case contains two space-separated integers A and B.
  • The third line of each test case contains a string S denoting the password.

Output format

For each test case, print the minimum replacements required in a new line. In the next line, print the updated lexicographically smallest password S.

Constraints

1T1000001N2000000A,BN (A+BN)

Sum of N over T testcases does not exceed 1e6

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first testcase, the minimum number of replacements required is 2. Some of the possible variants of password are "011122", "012112" and "111022". But the lexicographically smallest password is "011122".

For the second testcase, we need to do 4 replacements. The only possible variant of the password is "00000".

Editor Image

?