The variant of passwords
Practice
3.5 (13 votes)
Basic programming
Algorithms
Greedy algorithm
Problem
62% Success 5050 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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

\(1 \le T \le 100000\\ 1 \le N \le 200000\\ 0 \le A,B \le N\ (A+B \le N)\)

Sum of $$N$$ over $$T$$ testcases does not exceed $$1e6$$

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
39 votes
Tags:
Basic ProgrammingBit ManipulationImplementationString Manipulation
Points:30
16 votes
Tags:
Basic ProgrammingBasics of ImplementationEasyImplementation
Points:30
7 votes
Tags:
Data StructuresEasyImplementationapproved