Codeforces Round 1115 (Div. 2)
B
始终变化
Always Changing
Codeforces
1100
原题
← AC →

始终变化AI (CI翻译)

You are given a binary string $s$ of length $n$.

给定一个长度为 $n$ 的二进制字符串 $s$。AI (CI翻译)

A string is called alternating if no two adjacent characters are the same. For example, 0101, 1, and 01 are alternating, but 0110 is not.

如果一个字符串中任意两个相邻字符都不相同,则称它为交替串。例如,0101、1 和 01 都是交替串,但 0110 不是。AI (CI翻译)

You want to transform $s$ into an alternating string by performing the following operation any number of times (possibly zero):

你希望通过执行以下操作任意次(可以为零次),将 $s$ 变成交替串:AI (CI翻译)

  • Choose any character currently in the string and delete it.
  • 选择当前字符串中的任意一个字符并将其删除。
AI (CI翻译)

However, your sequence of operations must follow a rule: the characters you delete must strictly alternate. This means if the last character you deleted was 0, the next character you delete must be 1, and vice versa. Your very first deleted character can be either 0 or 1.

但是,你执行操作的顺序必须满足一条规则:删除的字符必须严格交替。也就是说,如果上一次删除的字符是 0,那么下一次删除的字符必须是 1,反之亦然。第一次删除的字符可以是 0 或 1。AI (CI翻译)

Find the minimum number of operations required to make $s$ an alternating string. If it is impossible to achieve this, output $-1$.

求将 $s$ 变成交替串所需的最少操作次数。如果无法实现,输出 $-1$。AI (CI翻译)

Input

输入格式

AI (CI翻译)

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

每个测试包含多个测试用例。第一行包含测试用例数量 $t$ ($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$.

每个测试用例的第一行包含一个整数 $n$ ($1 \le n \le 2 \cdot 10^5$),表示字符串 $s$ 的长度。AI (CI翻译)

The second line of each test case contains the binary string $s$ of length $n$, consisting only of the characters 0 and 1.

每个测试用例的第二行包含一个长度为 $n$ 的二进制字符串 $s$,仅由字符 0 和 1 组成。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output a single integer — the minimum number of operations required to make $s$ an alternating string, or $-1$ if it is impossible.

对于每个测试用例,输出一个整数,表示将 $s$ 变成交替串所需的最少操作次数;如果无法实现,则输出 $-1$。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, the string 0101 is already alternating. No operations are needed, so the answer is $0$.

在第一个测试用例中,字符串 0101 已经是交替串。不需要执行任何操作,因此答案为 $0$。AI (CI翻译)

In the second test case, the string is 111. To make it alternating, we must leave at most one 1, which requires deleting two 1s. However, our deletions must strictly alternate between 0 and 1. Since we have no 0s to delete, it is impossible to perform two deletions. The answer is $-1$.

在第二个测试用例中,字符串为 111。要使它变成交替串,最多只能留下一个 1,因此需要删除两个 1。但是,删除操作必须在 0 和 1 之间严格交替。由于没有 0 可供删除,所以无法执行两次删除。答案为 $-1$。AI (CI翻译)

In the third test case, the string is 100110. We can simply delete one 0 and one 1 to resolve the adjacent duplicates. Since we deleted exactly one 0 and one 1, the operations are perfectly balanced and valid. The remaining string is 1010, requiring exactly $2$ operations.

在第三个测试用例中,字符串为 100110。我们只需删除一个 0 和一个 1,即可消除相邻重复字符。由于恰好删除了一个 0 和一个 1,删除操作数量平衡,因此符合规则。剩下的字符串为 1010,共需要执行 $2$ 次操作。AI (CI翻译)

In the fourth test case, the string is 100010. To resolve the adjacent duplicates, we would naturally want to delete two 0s to form 1010. However, deleting two 0s and zero 1s violates the strict alternation rule (the difference in counts cannot exceed $1$). To fix this imbalance, we are forced to additionally delete a 1 from the edge of the string. We delete two 0s and one 1 (e.g., in the order 0, 1, 0), leaving the alternating string 010. This requires $3$ operations.

在第四个测试用例中,字符串为 100010。为了消除相邻重复字符,我们自然会想删除两个 0,得到 1010。但是,删除两个 0 且不删除任何 1 违反了严格交替规则(两种字符的删除次数之差不能超过 $1$)。为了弥补这种不平衡,我们不得不额外删除字符串边缘的一个 1。我们删除两个 0 和一个 1(例如,按 0、1、0 的顺序),留下交替串 010。这需要 $3$ 次操作。AI (CI翻译)

In the fifth test case, the string is 011110. Resolving the adjacent duplicates requires deleting three 1s. To balance this, we must additionally delete at least two 0s. Since our compressed string 010 only has two 0s (at its outer edges), we are forced to delete both of them, leaving a final string of just 1. We deleted three 1s and two 0s, which takes $5$ operations.

在第五个测试用例中,字符串为 011110。要消除相邻重复字符,需要删除三个 1。为了平衡删除次数,我们还至少要删除两个 0。由于压缩后的字符串 010 中只有两个 0(分别位于两端),我们不得不将它们都删除,最终只留下 1。我们删除了三个 1 和两个 0,共需要执行 $5$ 次操作。AI (CI翻译)

样例

Input #1
5
4
0101
3
111
6
100110
6
100010
6
011110
Output #1
0
-1
2
3
5
InputCopy OutputCopy
题目信息
题号
2252B
时间限制
1.5 s
内存限制
256 MB
标签
动态规划
贪心
数学
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。