[Solution] BinaryStringForces Codeforces Solution
You are given a binary string of length . We define a maximal substring as a substring that cannot be extended while keeping all elements equal. For example, in the string there are three maximal substrings: , and .
In one operation, you can select two maximal adjacent substrings. Since they are maximal and adjacent, it's easy to see their elements must have different values. Let be the length of the sequence of ones and be the length of the sequence of zeros. Then do the following:
- If , then replace selected zeros with ones.
- If , then replace selected ones with zeros.
As an example, for we make it , for we make it . We call a string being good if it can be turned into using the aforementioned operation any number of times (possibly, zero). Find the number of good substrings among all non-empty substrings of .
Each test consists of multiple test cases. The first line contains a single integer () — the number of test cases. The description of test cases follows.
The first line of each test case contains () — the length of the string .
The second line of each test case contains the binary string of length .
It is guaranteed that sum of across all test cases doesn't exceed .
For each test case, print a single integer — the number of good substrings.
Let's define a substring from index to index as .
For the first test case, the good substrings are:
- ,
- ,
- ,
- ,
- ,
- ,
- ,
- .
In the second test case, all substrings are good except .
In the third test case, all substrings are good.
No comments:
Post a Comment