CF1907C.Removal of Unattractive Pairs
普及-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Vlad found a string s consisting of n lowercase Latin letters, and he wants to make it as short as possible.
To do this, he can remove any pair of adjacent characters from s any number of times, provided they are different. For example, if s=racoon, then by removing one pair of characters he can obtain the strings coon, roon, raon, and raco, but he cannot obtain racn (because the removed letters were the same) or rcon (because the removed letters were not adjacent).
What is the minimum length Vlad can achieve by applying any number of deletions?
弗拉德找到了一个由 n 个小写拉丁字母组成的字符串 s,他希望将其尽可能缩短。
为此,他可以任意次地从 s 中删除任意一对相邻字符,但前提是这两个字符互不相同。例如,若 s=racoon,则通过删除一对字符,他可以得到字符串 coon、roon、raon 和 raco,但他无法得到 racn(因为被删去的两个字母相同),也无法得到 rcon(因为被删去的两个字母不相邻)。
通过任意次数的删除操作,弗拉德所能达到的最短长度是多少?
输入格式
The first line of the input contains a single integer t (1≤t≤104) — the number of test cases. Descriptions of the test cases follow.
The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of the string s.
The second line of each test case contains the string s consisting of n lowercase Latin letters.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
输入的第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105),表示字符串 s 的长度。
每个测试用例的第二行包含一个由 n 个小写拉丁字母组成的字符串 s。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case, output a single number—the minimum length of the string s, after removing pairs of adjacent characters with different values.
对于每个测试用例,输出一个数字——字符串 s 在删除所有相邻且值不同的字符对之后的最小长度。
输入输出样例
输入#1
10 4 aabc 5 abaca 10 avbvvcvvvd 7 abcdefg 5 dabbb 8 aacebeaa 7 bbbbacc 6 dacfcc 6 fdfcdc 9 dbdcfbbdc
输出#1
0 1 2 1 1 0 1 0 0 1
说明/提示
In the first test case of the example, you need to act as follows: "aabc" → "ac" → "". Note that with a different order of deletions, the string will not become empty.
在示例的第一个测试用例中,你需要按如下方式操作:“aabc” → “ac” → “”。注意,若以不同的顺序进行删除,字符串将无法变为空。
输入解题思路,AI测评打分。不知道怎么写?