[Solution] K-Flip CodeChef Solution | Solution CodeChef
Problem
You are given a binary string of length . You are also given an integer . You can do the following operation at most times:
- First, choose a substring of of length exactly . This substring shouldn't have been chosen in any of the previous operations.
- Then, flip all the characters of the chosen substring (i.e. change every to and vice versa).
Find the lexicographically smallest string possible after the operations if you do them in an optimal way.
Note:
- A substring is consecutive elements of a string. For eg. in the string "01011", "101" is a substring, but "111" is not a substring.
- A string is lexicographically smaller than string , if , where is the first index where and differ.
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- The first line of each test case contains two space-separated integers: , .
- The second line of each test case contains the string .
Output Format
For each test case, output on a new line the lexicographically smallest string that you can achieve.
Explanation:
Test case : Choose the substring and flip it. So now becomes . Then choose the substring and flip it. So now becomes . This is the lexicographically smallest string that can be achieved, and hence this is the answer.
Test case : Choose the substring and flip it. So now becomes . Then choose the substring and flip it. So now becomes . This is the lexicographically smallest string that can be achieved, and hence this is the answer.
Test case : Choose the substring and flip it. So now becomes . Then choose the substring and flip it. So now becomes . This is the lexicographically smallest string that can be achieved, and hence this is the answer.
Test case : Choose the substring and flip it. So now becomes . Then choose the substring and flip it. So now becomes . This is the lexicographically smallest string that can be achieved, and hence this is the answer.
No comments:
Post a Comment