CF2237D.Fullmetal Bitchemist

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A binary string is a string consisting only of characters 00 and 11. The two characters 00 and 11 are called opposite values.

Consider a binary string tt. Let ∣t∣|t| be the length of tt. When ∣t∣≥2|t| \ge 2, for every 1≤i<∣t∣1 \le i \lt |t|, the characters tit_i and ti+1t_{i+1} are adjacent.

A binary string tt is called beautiful if it can be reduced to a string of length exactly 11 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\mathtt{10001} can become 1101\mathtt{1101} by replacing the first adjacent pair 00\mathtt{00} with 1\mathtt{1}. Then it can become 001\mathtt{001}, then 11\mathtt{11}, and finally 0\mathtt{0}. Therefore, 10001\mathtt{10001} is beautiful.

On the other hand, 111\mathtt{111} is not beautiful. After one operation, it becomes 01\mathtt{01}, and then no operation can be applied.

You are given a binary string ss. Compute the number of non-empty beautiful substrings∗^{\text{∗}} of ss.

∗^{\text{∗}}A string aa is a substring of a string bb if aa can be obtained from bb by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

二进制字符串是指仅由字符 00 和 11 组成的字符串。字符 00 和 11 被称为互异值(opposite values)。

考虑一个二进制字符串 tt。记 ∣t∣|t| 为 tt 的长度。当 ∣t∣≥2|t| \ge 2 时,对每个满足 1≤i<∣t∣1 \le i \lt |t| 的下标 ii,字符 tit_i 和 ti+1t_{i+1} 称为相邻。

若一个二进制字符串 tt 可通过任意多次(包括零次)执行如下操作而化简为长度恰好为 11 的字符串,则称其为优美的(beautiful):

  • 选择一对相等的相邻字符,移除这两个字符,并在它们原来的位置插入一个与其值互异的字符。

例如,字符串 10001\mathtt{10001} 可通过将首对相邻字符 00\mathtt{00} 替换为 1\mathtt{1},变为 1101\mathtt{1101};接着可变为 001\mathtt{001},再变为 11\mathtt{11},最终变为 0\mathtt{0}。因此,10001\mathtt{10001} 是优美的。

另一方面,111\mathtt{111} 并不优美:执行一次操作后变为 01\mathtt{01},此后无法再执行任何操作。

现给定一个二进制字符串 ss,请计算 ss 中非空优美子串∗^{\text{∗}} 的个数。

∗^{\text{∗}} 字符串 aa 是字符串 bb 的子串,当且仅当 aa 可通过从 bb 的开头删除若干(可能为零或全部)字符、并从 bb 的末尾删除若干(可能为零或全部)字符而得到。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤1061 \le n \le 10^6) — the length of ss.

The second line of each test case contains a binary string ss of length nn.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1061 \le n \le 10^6)—— 字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss。

保证所有测试用例的 nn 之和不超过 10610^6。

输出格式

For each test case, output a single integer — the number of beautiful substrings of ss.

对于每个测试用例,输出一个整数——字符串 ss 的优美子串的数量。

输入输出样例

  • 输入#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\mathtt{0}, which is already a binary string of length 11. Therefore, it is beautiful.

In the second test case, the beautiful substrings are 0\mathtt{0} and 1\mathtt{1}. The substring 01\mathtt{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]=0s[1,1]=\mathtt{0};
  • s[2,2]=1s[2,2]=\mathtt{1};
  • s[3,3]=0s[3,3]=\mathtt{0};
  • s[4,4]=0s[4,4]=\mathtt{0};
  • s[5,5]=1s[5,5]=\mathtt{1};
  • s[3,4]=00s[3,4]=\mathtt{00}, which can become 1\mathtt{1};
  • s[2,4]=100s[2,4]=\mathtt{100}, which can become 11\mathtt{11}, then 0\mathtt{0};
  • s[3,5]=001s[3,5]=\mathtt{001}, which can become 11\mathtt{11}, then 0\mathtt{0};
  • s[1,4]=0100s[1,4]=\mathtt{0100}, which can become 011\mathtt{011}, then 00\mathtt{00}, then 1\mathtt{1};
  • s[1,5]=01001s[1,5]=\mathtt{01001}, which can become 0111\mathtt{0111}, then 001\mathtt{001}, then 11\mathtt{11}, and finally 0\mathtt{0}.

Thus, there are 1010 beautiful substrings.

在第一个测试用例中,唯一的非空子串是 0\mathtt{0},它本身就是一个长度为 11 的二进制字符串,因此它是优美的。

在第二个测试用例中,优美的子串是 0\mathtt{0} 和 1\mathtt{1}。子串 01\mathtt{01} 不优美,因为它的两个字符不相等,因此无法执行任何操作。

在第三个测试用例中,优美的子串有:

  • s[1,1]=0s[1,1]=\mathtt{0};
  • s[2,2]=1s[2,2]=\mathtt{1};
  • s[3,3]=0s[3,3]=\mathtt{0};
  • s[4,4]=0s[4,4]=\mathtt{0};
  • s[5,5]=1s[5,5]=\mathtt{1};
  • s[3,4]=00s[3,4]=\mathtt{00},它可以变为 1\mathtt{1};
  • s[2,4]=100s[2,4]=\mathtt{100},它可以变为 11\mathtt{11},再变为 0\mathtt{0};
  • s[3,5]=001s[3,5]=\mathtt{001},它可以变为 11\mathtt{11},再变为 0\mathtt{0};
  • s[1,4]=0100s[1,4]=\mathtt{0100},它可以变为 011\mathtt{011},再变为 00\mathtt{00},最后变为 1\mathtt{1};
  • s[1,5]=01001s[1,5]=\mathtt{01001},它可以变为 0111\mathtt{0111},再变为 001\mathtt{001},然后变为 11\mathtt{11},最终变为 0\mathtt{0}。

因此,共有 1010 个优美的子串。

输入解题思路,AI测评打分。不知道怎么写?

首页