[Solution] Binary Substitution CodeChef Solution
Problem
Chef has binary string of length .
Chef can perform the following operation on the string:
- Choose a contiguous subarray such that the count of set bits in the subarray is equal to the count of unset bits in the subarray.
- Replace the chosen subarray with either a set bit or an unset bit.
Chef wants to reduce the string to minimum possible length using minimum number of given operations. Help Chef by telling him the minimum length and also the operations required to obtain that. If there are multiple ways to obtain the answer, print any.
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 a single integer , denoting the length of the binary string.
- The second line of each test case contains a binary string .
Output Format
For each test case, output lines:
- The first line should contain two space-separated integers and , denoting the minimum length that can be obtained and the minimum number of operations required to obtain it respectively.
- Then, lines follow, where the line denotes the operation:
- Each operation is denoted using three space separated integers , , and .
The integers and denote the chosen substring such that and the substring has equal count of set and unset bits. Note that, denotes the length of the current string.
The integer denotes the bit with which the substring is replaced.
Explanation:
Test case : We can reduce the string to a string of length . We require only operation to do so:
- Choose and . We chose the substring which contains set bits and unset bits. We can replace the chosen substring with bit .
👇👇👇👇👇
Test case : The given string is of length . Thus, we cannot reduce it any further.
Test case : We can reduce the string to a string of length . We require operations to do so:
- Operation : Choose and . We chose the substring which contains set bits and unset bits. We can replace the chosen substring with bit . Thus, the string after this operation is .
- Operation : Choose and . We chose the substring which contains set bit and unset bit. We can replace the chosen substring with bit . Thus, the string after this operation is .
No comments:
Post a Comment