Codeforces Round 741 (Div. 2)
E
Rescue Niwen!
Codeforces
2500
原题
← D2F →
翻译中…

Morning desert sun horizonRise above the sands of time...Fates Warning, "Exodus"

翻译中…

After crossing the Windswept Wastes, Ori has finally reached the Windtorn Ruins to find the Heart of the Forest! However, the ancient repository containing this priceless Willow light did not want to open!

翻译中…

Ori was taken aback, but the Voice of the Forest explained to him that the cunning Gorleks had decided to add protection to the repository.

翻译中…

The Gorleks were very fond of the "string expansion" operation. They were also very fond of increasing subsequences.

翻译中…

Suppose a string $s_1s_2s_3 \ldots s_n$ is given. Then its "expansion" is defined as the sequence of strings $s_1$, $s_1 s_2$, ..., $s_1 s_2 \ldots s_n$, $s_2$, $s_2 s_3$, ..., $s_2 s_3 \ldots s_n$, $s_3$, $s_3 s_4$, ..., $s_{n-1} s_n$, $s_n$. For example, the "expansion" the string 'abcd' will be the following sequence of strings: 'a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd'.

翻译中…

To open the ancient repository, Ori must find the size of the largest increasing subsequence of the "expansion" of the string $s$. Here, strings are compared lexicographically.

翻译中…

Help Ori with this task!

翻译中…

A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds:

翻译中…
  • $a$ is a prefix of $b$, but $a \ne b$;
  • in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$.
翻译中…

Input

翻译中…

Each test contains multiple test cases.

翻译中…

The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.

翻译中…

The first line of each test case contains one positive integer $n$ ($1 \le n \le 5000$) — length of the string.

翻译中…

The second line of each test case contains a non-empty string of length $n$, which consists of lowercase latin letters.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$.

翻译中…

Output

翻译中…

For every test case print one non-negative integer — the answer to the problem.

翻译中…

Note

翻译中…

In first test case the "expansion" of the string is: 'a', 'ac', 'acb', 'acba', 'acbac', 'c', 'cb', 'cba', 'cbac', 'b', 'ba', 'bac', 'a', 'ac', 'c'. The answer can be, for example, 'a', 'ac', 'acb', 'acba', 'acbac', 'b', 'ba', 'bac', 'c'.

翻译中…

样例

Input #1
7
5
acbac
8
acabacba
12
aaaaaaaaaaaa
10
abacabadac
8
dcbaabcd
3
cba
6
sparky
Output #1
9
17
12
29
14
3
9
InputCopy OutputCopy
题目信息
题号
1562E
时间限制
2 s
内存限制
512 MB
标签
动态规划
贪心
后缀结构
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。