CF BUDDY
← Problems·

1659B · Bit Flipping

1300 · bitmasks, constructive algorithms, greedy

Problem: You are given a binary string of length nn. You have exactly kk moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped (00 becomes 11, 11 becomes 00). You need to output the lexicographically largest string that you can get after using all kk moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.

A binary string aa is lexicographically larger than a binary string bb of the same length, if and only if the following holds:

  • in the first position where aa and bb differ, the string aa contains a 11, and the string bb contains a 00.

Input Format: The first line contains a single integer tt (1t10001 \le t \le 1000)  — the number of test cases.

Each test case has two lines. The first line has two integers nn and kk (1n21051 \leq n \leq 2 \cdot 10^5; 0k1090 \leq k \leq 10^9).

The second line has a binary string of length nn, each character is either 00 or 11.

The sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output two lines.

The first line should contain the lexicographically largest string you can obtain.

The second line should contain nn integers f1,f2,,fnf_1, f_2, \ldots, f_n, where fif_i is the number of times the ii-th bit is selected. The sum of all the integers must be equal to kk.

Note: Here is the explanation for the first testcase. Each step shows how the binary string changes in a move.

  • Choose bit 11: 100001111110\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}.
  • Choose bit 44: 111110000101\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}.
  • Choose bit 44: 000101111110\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}.

Sample Cases

Case 1

Input

6
6 3
100001
6 4
100011
6 0
000000
6 1
111001
6 11
101100
6 12
001110

Output

111110
1 0 0 2 0 0 
111110
0 1 1 1 0 1 
000000
0 0 0 0 0 0 
100110
1 0 0 0 0 0 
111111
1 2 1 3 0 4 
111110
1 1 4 2 0 4

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback