CF2145C.Monocarp's String

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Monocarp has a string ss of length nn, consisting of the letters 'a' and 'b'. He wants to remove some (possibly zero) number of consecutive letters from his string in such a way that the number of letters 'a' and 'b' in the resulting string becomes equal. Monocarp can start removing letters from any position in the string ss.

Monocarp really likes his string ss, so he wants to remove as few consecutive letters from it as possible.

Your task is to determine the minimum number of consecutive letters from the string ss that need to be removed so that the number of letters 'a' and 'b' in the resulting string becomes equal. If it is necessary to remove all letters from the string ss (i.e., make it empty), report this.

Monocarp 有一个长度为 nn 的字符串 ss,仅由字母 'a' 和 'b' 组成。他希望从该字符串中删除若干(可能为零)个连续的字符,使得剩余字符串中字母 'a' 和 'b' 的数量相等。Monocarp 可以从字符串 ss 中任意位置开始删除字符。

Monocarp 非常喜欢他的字符串 ss,因此他希望删除的连续字符数量尽可能少。

你的任务是确定:为使剩余字符串中 'a' 和 'b' 的数量相等,需要从字符串 ss 中删除的最少连续字符数。如果必须删除字符串 ss 中的所有字符(即使其变为空串),请报告这一情况。

输入格式

The first line contains one integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

Each test case consists of two lines:

  • the first line contains one integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the number of characters in the string ss;
  • the second line contains the string ss of length nn, consisting of the letters 'a' and/or 'b'.

Additional constraint on the input: the sum of values of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例由两行组成:

  • 第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 字符串 ss 的长度;
  • 第二行包含一个长度为 nn 的字符串 ss,仅由字符 'a' 和/或 'b' 组成。

输入的额外约束:所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print the answer as follows:

If in order to make the number of letters 'a' and 'b' equal, it is necessary to remove all letters from the string ss, output −1-1.

Otherwise, output the minimum number of consecutive letters that Monocarp needs to remove from his string ss so that the number of letters 'a' and 'b' becomes equal.

对于每个测试用例,按如下方式输出答案:

如果为了使字母 'a' 和 'b' 的数量相等,必须从字符串 ss 中删除所有字母,则输出 −1-1。

否则,输出 Monocarp 需要从他的字符串 ss 中删除的最少连续字母个数,使得字母 'a' 和 'b' 的数量相等。

输入输出样例

  • 输入#1

    5
    5
    bbbab
    6
    bbaaba
    4
    aaaa
    12
    aabbaaabbaab
    5
    aabaa

    输出#1

    3
    0
    -1
    2
    -1

说明/提示

In the first example, Monocarp needs to remove the first three letters from his string. After that, his string will become "ab". In this string, there is one letter 'a' and one letter 'b'.

In the second example, the given string has three letters 'a' and three letters 'b', so nothing needs to be removed.

In the third example, all letters of the string need to be removed, as there are no letters 'b', so −1-1 should be printed.

In the fourth example, Monocarp can, for example, remove the fifth and sixth letters from his string. Then his string will become "aabbabbaab". In this string, there are five letters 'a' and five letters 'b'.

In the fifth example, all letters of the string need to be removed to make the number of letters 'a' and 'b' equal, so −1-1 should be printed.

在第一个例子中,Monocarp 需要从他的字符串中删除前三个字母。之后,他的字符串将变为 "ab"。在此字符串中,有一个字母 'a' 和一个字母 'b'。

在第二个例子中,给定字符串包含三个字母 'a' 和三个字母 'b',因此无需删除任何字符。

在第三个例子中,字符串中的所有字母都需要被删除,因为其中不含字母 'b',因此应输出 −1-1。

在第四个例子中,Monocarp 可以(例如)删除他字符串中的第五个和第六个字母。之后,他的字符串将变为 "aabbabbaab"。在此字符串中,有五个字母 'a' 和五个字母 'b'。

在第五个例子中,为使字母 'a' 和 'b' 的数量相等,字符串中的所有字母都需要被删除,因此应输出 −1-1。

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

首页