CF2254B.Evanescent
入门
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Let f(s) be the compressed version of a string s, formed by replacing every maximal contiguous block of identical characters with a single copy of that character. For example, f("aabbcc"$) \ = \ $"abc".
Let ∣s∣ denote the length of a string s. Following this, ∣f(s)∣ denotes the length of the compressed string. For example:
- ∣f("aabbcc")∣= ∣"abc"∣ $ = 3$
- If the string is empty, its length is 0.
Yousef has given you a string s consisting of n lowercase Latin letters. You must delete exactly one character si (2≤i≤n−1) to form a new string s′, and then find the minimum possible value of ∣f(s′)∣.
Note that you cannot delete s1 or sn.
设 f(s) 为字符串 s 的压缩版本,其定义为:将 s 中每个最长的连续相同字符块替换为该字符的一个副本。例如,f("aabbcc"$) \ = \ $"abc"。
用 ∣s∣ 表示字符串 s 的长度。据此,∣f(s)∣ 表示压缩后字符串的长度。例如:
- ∣f("aabbcc")∣= ∣"abc"∣ $ = 3$
- 若字符串为空,则其长度为 0。
优素福给你一个由 n 个小写拉丁字母组成的字符串 s。你必须恰好删除一个字符 si(其中 2≤i≤n−1),从而得到新字符串 s′,然后求出 ∣f(s′)∣ 的最小可能值。
注意:你不能删除 s1 或 sn。
输入格式
The first line contains an integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains an integer n (3≤n≤2⋅105) — the length of the string.
The second line of each test case contains a string s (∣s∣=n), consisting of 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(3≤n≤2⋅105)—— 字符串的长度。
每个测试用例的第二行包含一个字符串 s(∣s∣=n),由小写拉丁字母组成。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output a single integer — the minimum possible length of the resulting compressed string after deleting one character.
对于每个测试用例,输出一个整数——删除一个字符后所得压缩字符串的最小可能长度。
输入输出样例
输入#1
9 3 abb 3 aab 3 abc 4 abaa 4 abba 5 eeeee 6 yyssee 7 abacaba 18 goodluckandhavefun
输出#1
2 2 2 1 3 1 3 5 16
说明/提示
In the first test case, we can only delete the character $s_2 = $ 'b', producing a string $s' = $ "ab", with ∣f(s′)∣=2. Therefore, 2 is the minimum length achievable.
In the fourth test case, we can delete the character $s_2 = $ 'b'. The resulting string is $s' = $ "aaa" with $f(s') = $ "a", so ∣f(s′)∣=1.
In the sixth test case, deleting any valid character results in $f(s') = $ "e" and ∣f(s′)∣=1.
In the eighth test case, we can delete the character $s_4 = $ 'c'. The resulting string is $s' = $ "abaaba" with $f(s') = $ "ababa" and ∣f(s′)∣=5.
在第一个测试用例中,我们只能删除字符 $s_2 = $ 'b',得到字符串 $s' = $ "ab",此时 ∣f(s′)∣=2。因此,可达到的最小长度为 2。
在第四个测试用例中,我们可以删除字符 $s_2 = $ 'b'。所得字符串为 $s' = $ "aaa",且 $f(s') = $ "a",故 ∣f(s′)∣=1。
在第六个测试用例中,删除任意一个合法字符均使得 $f(s') = $ "e",且 ∣f(s′)∣=1。
在第八个测试用例中,我们可以删除字符 $s_4 = $ 'c'。所得字符串为 $s' = $ "abaaba",且 $f(s') = $ "ababa",故 ∣f(s′)∣=5。
输入解题思路,AI测评打分。不知道怎么写?