CF2254B.Evanescent

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Let f(s)f(s) be the compressed version of a string ss, formed by replacing every maximal contiguous block of identical characters with a single copy of that character. For example, f(f("aabbcc"$) \ = \ $"abc".

Let ∣s∣|s| denote the length of a string ss. Following this, ∣f(s)∣|f(s)| denotes the length of the compressed string. For example:

  • ∣f(|f("aabbcc")∣=)| = ∣|"abc"∣| $ = 3$
  • If the string is empty, its length is 00.

Yousef has given you a string ss consisting of nn lowercase Latin letters. You must delete exactly one character sis_i (2≤i≤n−12 \le i \le n - 1) to form a new string s′s', and then find the minimum possible value of ∣f(s′)∣|f(s')|.

Note that you cannot delete s1s_1 or sns_n.

设 f(s)f(s) 为字符串 ss 的压缩版本,其定义为:将 ss 中每个最长的连续相同字符块替换为该字符的一个副本。例如,f(f("aabbcc"$) \ = \ $"abc"。

用 ∣s∣|s| 表示字符串 ss 的长度。据此,∣f(s)∣|f(s)| 表示压缩后字符串的长度。例如:

  • ∣f(|f("aabbcc")∣=)| = ∣|"abc"∣| $ = 3$
  • 若字符串为空,则其长度为 00。

优素福给你一个由 nn 个小写拉丁字母组成的字符串 ss。你必须恰好删除一个字符 sis_i(其中 2≤i≤n−12 \le i \le n - 1),从而得到新字符串 s′s',然后求出 ∣f(s′)∣|f(s')| 的最小可能值。

注意:你不能删除 s1s_1 或 sns_n。

输入格式

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

The first line of each test case contains an integer nn (3≤n≤2⋅1053 \le n \le 2 \cdot 10^5) — the length of the string.

The second line of each test case contains a string ss (∣s∣=n|s| = n), consisting of lowercase Latin letters.

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

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

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053 \le n \le 2 \cdot 10^5)—— 字符串的长度。

每个测试用例的第二行包含一个字符串 ss(∣s∣=n|s| = n),由小写拉丁字母组成。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

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|f(s')| = 2. Therefore, 22 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|f(s')| = 1.

In the sixth test case, deleting any valid character results in $f(s') = $ "e" and ∣f(s′)∣=1|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|f(s')| = 5.

在第一个测试用例中,我们只能删除字符 $s_2 = $ 'b',得到字符串 $s' = $ "ab",此时 ∣f(s′)∣=2|f(s')| = 2。因此,可达到的最小长度为 22。

在第四个测试用例中,我们可以删除字符 $s_2 = $ 'b'。所得字符串为 $s' = $ "aaa",且 $f(s') = $ "a",故 ∣f(s′)∣=1|f(s')| = 1。

在第六个测试用例中,删除任意一个合法字符均使得 $f(s') = $ "e",且 ∣f(s′)∣=1|f(s')| = 1。

在第八个测试用例中,我们可以删除字符 $s_4 = $ 'c'。所得字符串为 $s' = $ "abaaba",且 $f(s') = $ "ababa",故 ∣f(s′)∣=5|f(s')| = 5。

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

首页