#P10841. 【MX-J2-T2】Turtle and Strings

    ID: 12265 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>动态规划 DP贪心O2优化梦熊比赛

【MX-J2-T2】Turtle and Strings

Background

Original problem link: https://oier.team/problems/J2C.

Problem Description

You are given a string ss consisting only of lowercase letters.

A sequence of strings t1,t2,,tkt_1, t_2, \ldots, t_k is valid if and only if:

  • s=t1+t2++tks = t_1 + t_2 + \cdots + t_k, where ++ means string concatenation.
  • 1ik1,titi+1\forall 1 \le i \le k - 1, t_i \ne t_{i + 1}.

Find the maximum possible length kk of a valid string sequence.

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, the number of test cases.

For each test case:

The first line contains a positive integer nn, the length of the string.

The second line contains a string ss of length nn consisting only of lowercase letters.

Output Format

For each test case, output one line containing an integer, representing the maximum length of a valid string sequence.

4
3
abc
5
aabbb
6
aaaaaa
10
pppqqppppq

3
3
4
7

Hint

Sample Explanation

In the first test case, a valid sequence with maximum length is [a,b,c][\texttt{a}, \texttt{b}, \texttt{c}].

In the second test case, a valid sequence with maximum length is [a,abb,b][\texttt{a}, \texttt{abb}, \texttt{b}].

In the third test case, a valid sequence with maximum length is [a,aa,a,aa][\texttt{a}, \texttt{aa}, \texttt{a}, \texttt{aa}].

Constraints

This problem uses bundled testdata and enables subtask dependencies.

Subtask ID Score nn \le n\sum n \le Special Property Subtask Dependencies
11 1818 99 10410^4 None None
22 2121 5050 10310^3 11
33 1212 10610^6 s1=s2==sns_1 = s_2 = \cdots = s_n None
44 2323 There exists exactly one position 1in11 \le i \le n - 1 such that sisi+1s_i \ne s_{i + 1}
55 2626 None 1,2,3,41, 2, 3, 4

For all testdata, 1T1051 \le T \le 10^5, 1n,n1061 \le n, \sum n \le 10^6, and ss consists only of lowercase letters.

Translated by ChatGPT 5