CF1791D.Distinct Split

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Let's denote the f(x)f(x) function for a string xx as the number of distinct characters that the string contains. For example f(abc)=3f(\texttt{abc}) = 3, f(bbbbb)=1f(\texttt{bbbbb}) = 1, and f(babacaba)=3f(\texttt{babacaba}) = 3.

Given a string ss, split it into two non-empty strings aa and bb such that f(a)+f(b)f(a) + f(b) is the maximum possible. In other words, find the maximum possible value of f(a)+f(b)f(a) + f(b) such that a+b=sa + b = s (the concatenation of string aa and string bb is equal to string ss).

我们用 f(x)f(x) 表示字符串 xx 的不同字符个数。例如:f(abc)=3f(\texttt{abc}) = 3,f(bbbbb)=1f(\texttt{bbbbb}) = 1,f(babacaba)=3f(\texttt{babacaba}) = 3。

给定一个字符串 ss,将其分割为两个非空字符串 aa 和 bb,使得 f(a)+f(b)f(a) + f(b) 尽可能大。换言之,求满足 a+b=sa + b = s(即字符串 aa 与字符串 bb 的拼接等于字符串 ss)时,f(a)+f(b)f(a) + f(b) 的最大可能值。

输入格式

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer nn (2≤n≤2⋅1052 \leq n \leq 2\cdot10^5) — the length of the string ss.

The second line contains the string ss, consisting of lowercase English letters.

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

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \leq n \leq 2\cdot10^5),表示字符串 ss 的长度。

每个测试用例的第二行包含字符串 ss,由小写英文字母组成。

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

输出格式

For each test case, output a single integer — the maximum possible value of f(a)+f(b)f(a) + f(b) such that a+b=sa + b = s.

对于每个测试用例,输出一个整数——满足 a+b=sa + b = s 的 f(a)+f(b)f(a) + f(b) 的最大可能值。

输入输出样例

  • 输入#1

    5
    2
    aa
    7
    abcabcd
    5
    aaaaa
    10
    paiumoment
    4
    aazz

    输出#1

    2
    7
    2
    10
    3

说明/提示

For the first test case, there is only one valid way to split aa\texttt{aa} into two non-empty strings a\texttt{a} and a\texttt{a}, and f(a)+f(a)=1+1=2f(\texttt{a}) + f(\texttt{a}) = 1 + 1 = 2.

For the second test case, by splitting abcabcd\texttt{abcabcd} into abc\texttt{abc} and abcd\texttt{abcd} we can get the answer of f(abc)+f(abcd)=3+4=7f(\texttt{abc}) + f(\texttt{abcd}) = 3 + 4 = 7 which is maximum possible.

For the third test case, it doesn't matter how we split the string, the answer will always be 22.

对于第一个测试用例,将字符串 aa\texttt{aa} 拆分为两个非空字符串 a\texttt{a} 和 a\texttt{a} 只有一种合法方式,且 f(a)+f(a)=1+1=2f(\texttt{a}) + f(\texttt{a}) = 1 + 1 = 2。

对于第二个测试用例,将字符串 abcabcd\texttt{abcabcd} 拆分为 abc\texttt{abc} 和 abcd\texttt{abcd},可得到 f(abc)+f(abcd)=3+4=7f(\texttt{abc}) + f(\texttt{abcd}) = 3 + 4 = 7,这是可能的最大值。

对于第三个测试用例,无论以何种方式拆分该字符串,答案恒为 22。

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

首页