CF2237D.Fullmetal Bitchemist
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A binary string is a string consisting only of characters 0 and 1. The two characters 0 and 1 are called opposite values.
Consider a binary string t. Let ∣t∣ be the length of t. When ∣t∣≥2, for every 1≤i<∣t∣, the characters ti and ti+1 are adjacent.
A binary string t is called beautiful if it can be reduced to a string of length exactly 1 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 10001 can become 1101 by replacing the first adjacent pair 00 with 1. Then it can become 001, then 11, and finally 0. Therefore, 10001 is beautiful.
On the other hand, 111 is not beautiful. After one operation, it becomes 01, and then no operation can be applied.
You are given a binary string s. Compute the number of non-empty beautiful substrings∗ of s.
∗A string a is a substring of a string b if a can be obtained from b by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
二进制字符串是指仅由字符 0 和 1 组成的字符串。字符 0 和 1 被称为互异值(opposite values)。
考虑一个二进制字符串 t。记 ∣t∣ 为 t 的长度。当 ∣t∣≥2 时,对每个满足 1≤i<∣t∣ 的下标 i,字符 ti 和 ti+1 称为相邻。
若一个二进制字符串 t 可通过任意多次(包括零次)执行如下操作而化简为长度恰好为 1 的字符串,则称其为优美的(beautiful):
- 选择一对相等的相邻字符,移除这两个字符,并在它们原来的位置插入一个与其值互异的字符。
例如,字符串 10001 可通过将首对相邻字符 00 替换为 1,变为 1101;接着可变为 001,再变为 11,最终变为 0。因此,10001 是优美的。
另一方面,111 并不优美:执行一次操作后变为 01,此后无法再执行任何操作。
现给定一个二进制字符串 s,请计算 s 中非空优美子串∗ 的个数。
∗ 字符串 a 是字符串 b 的子串,当且仅当 a 可通过从 b 的开头删除若干(可能为零或全部)字符、并从 b 的末尾删除若干(可能为零或全部)字符而得到。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤106) — the length of s.
The second line of each test case contains a binary string s of length n.
It is guaranteed that the sum of n over all test cases does not exceed 106.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤106)—— 字符串 s 的长度。
每个测试用例的第二行包含一个长度为 n 的二进制字符串 s。
保证所有测试用例的 n 之和不超过 106。
输出格式
For each test case, output a single integer — the number of beautiful substrings of s.
对于每个测试用例,输出一个整数——字符串 s 的优美子串的数量。
输入输出样例
输入#1
10 1 0 2 01 5 01001 3 001 6 011110 9 010110110 12 010000101001 16 1010011010010110 20 11110101101101001110 30 000101100011111001111100000010
输出#1
1 2 10 5 15 30 47 81 139 316
说明/提示
In the first test case, the only non-empty substring is 0, which is already a binary string of length 1. Therefore, it is beautiful.
In the second test case, the beautiful substrings are 0 and 1. The substring 01 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:
- s[1,1]=0;
- s[2,2]=1;
- s[3,3]=0;
- s[4,4]=0;
- s[5,5]=1;
- s[3,4]=00, which can become 1;
- s[2,4]=100, which can become 11, then 0;
- s[3,5]=001, which can become 11, then 0;
- s[1,4]=0100, which can become 011, then 00, then 1;
- s[1,5]=01001, which can become 0111, then 001, then 11, and finally 0.
Thus, there are 10 beautiful substrings.
在第一个测试用例中,唯一的非空子串是 0,它本身就是一个长度为 1 的二进制字符串,因此它是优美的。
在第二个测试用例中,优美的子串是 0 和 1。子串 01 不优美,因为它的两个字符不相等,因此无法执行任何操作。
在第三个测试用例中,优美的子串有:
- s[1,1]=0;
- s[2,2]=1;
- s[3,3]=0;
- s[4,4]=0;
- s[5,5]=1;
- s[3,4]=00,它可以变为 1;
- s[2,4]=100,它可以变为 11,再变为 0;
- s[3,5]=001,它可以变为 11,再变为 0;
- s[1,4]=0100,它可以变为 011,再变为 00,最后变为 1;
- s[1,5]=01001,它可以变为 0111,再变为 001,然后变为 11,最终变为 0。
因此,共有 10 个优美的子串。
输入解题思路,AI测评打分。不知道怎么写?