C. Get the Long Binary Number time limit per test1 second memory limit per test256…
- User
C. Get the Long Binary Number time limit per test1 second memory limit per test256 megabytes Consider a binary number x .Define c0(x) as the number of 0 in x ,c1(x) as the number of 1 in x .
For example,x=10100 ,we can get c0(x)=3,c1(x)=2 .
Now you're given a binary number y (without leading zero) of length n and an integer k .Your task is find the smallest binary number x (without leading zero),which satisfies:
y≤x ; c0(x)−c1(x)=k . Input The first line of input will contain a single integer t(1≤t≤105) , denoting the number of test cases.
Each test case consists of two line of input.
The first line contains two integers n,k(1≤n≤105,−105≤k≤105) .
The second line contains a binary number y (without leading zero) of length n .
The sum of n,|k| over all test cases will not exceed 4∗105 .
Output For each test case,output on a new line — the smallest binary number x (without leading zero),which satisfies:
y≤x ; c0(x)−c1(x)=k . Example Input 5 1 -1 0 6 -4 110100 6 1 100000 7 1 1110000 10 -2 1011010111 Output 1 110111 1000011 1110000 1011011001 First,be careful when y=0 and k=1 :)
Consider the answer with a length greater than n , which is always 10...01...1 .The construction is not hard.
What about the answer with length n ?
Consider setting numbers from the highest bit to the lowest bit and current bit is i .If yi=1 ,we must set xi=1 .Otherwise(yi=0 ),we either do set xi=0 and go to the lower bit,or do set xi=1 , xi+1xi+2...xn=0...01...1 and break.
For yi=0 ,when can we do set xi=0 and go to the lower bit?The condition is re1>pre1i+1 or re1==pre1i+1 and pre1i+1==tot1i+1 .re1 is the current remaining number of 1′s ,pre1i+1 is the length of the continuous 1 prefix of yi+1yi+2...yn ,and tot1i+1 is the total of 1 of yi+1yi+2...yn . make this solution in c++
- Assistant