[Solution] Maximize 1s CodeChef Solution
Problem
You are given a binary string . You are allowed to perform the following operation at most once:
- Pick some substring of
- Flip all the values in this substring, i.e, convert to and vice versa
For example, if , you can pick the underlined substring and flip it to obtain .
For the substring of consisting of all the positions from to , we define a function to be the number of 's in this substring. For example, if , then and (the respective substrings are and ).
If you perform the given operation optimally, find the maximum possible value of
that can be obtained. Note that the substring flip operation can be performed at most once.
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- Each test case consists of single line of input, containing a binary string .
Output Format
For each test case, output on a new line the maximum possible value of that can be obtained.
Explanation:
Test case : There is no need to apply the operation since everything is already a . The answer is thus the sum of:
which is .
Test case : Flip the entire string to obtain , whose answer has been computed above.
Test case : Flip the entire string to obtain . The sum of across all substrings is now , which is the maximum possible.
No comments:
Post a Comment