#CF2237D. 钢之位运算术师 / D. Fullmetal Bitchemist
钢之位运算术师 / D. Fullmetal Bitchemist
D. Fullmetal Bitchemist
Source: Codeforces 2237D
Contest: Order Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)
Time limit: 2 seconds
Memory limit: 256 megabytes
Statement
A binary string is a string consisting only of characters and . The two characters and are called opposite values.
Consider a binary string . Let be the length of . When , for every , the characters and are adjacent.
A binary string is called beautiful if it can be reduced to a string of length exactly by applying the following operation any number of times, possibly zero:
- Choose two equal adjacent characters, remove both of them, and insert one character with the opposite value in their place.
For example, the string can become by replacing the first adjacent pair with . Then it can become , then , and finally . Therefore, is beautiful.
On the other hand, is not beautiful. After one operation, it becomes , and then no operation can be applied.
You are given a binary string . Compute the number of non-empty beautiful substrings of .
A string is a substring of a string if can be obtained from by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains an integer () — the length of .
The second line of each test case contains a binary string of length .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the number of beautiful substrings of .
Note
In the first test case, the only non-empty substring is , which is already a binary string of length . Therefore, it is beautiful.
In the second test case, the beautiful substrings are and . The substring is not beautiful, because its two characters are not equal, so no operation can be applied.
In the third test case, the beautiful substrings are:
- ;
- ;
- ;
- ;
- ;
- , which can become ;
- , which can become , then ;
- , which can become , then ;
- , which can become , then , then ;
- , which can become , then , then , and finally .
Thus, there are beautiful substrings.
Examples
10
1
0
2
01
5
01001
3
001
6
011110
9
010110110
12
010000101001
16
1010011010010110
20
11110101101101001110
30
000101100011111001111100000010
1
2
10
5
15
30
47
81
139
316