CF1562E.Rescue Niwen!

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Morning desert sun horizon

Rise 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 s1s2s3…sns_1s_2s_3 \ldots s_n is given. Then its "expansion" is defined as the sequence of strings s1s_1, s1s2s_1 s_2, ..., s1s2…sns_1 s_2 \ldots s_n, s2s_2, s2s3s_2 s_3, ..., s2s3…sns_2 s_3 \ldots s_n, s3s_3, s3s4s_3 s_4, ..., sn−1sns_{n-1} s_n, sns_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 ss. Here, strings are compared lexicographically.

Help Ori with this task!

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

  • aa is a prefix of bb, but a≠ba \ne b;
  • in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.

清晨沙漠中的太阳,地平线之上

跃升于时光之沙……

命运警示乐队,《出埃及记》

在穿越了风蚀荒原之后,奥里终于抵达了风裂遗迹,寻找森林之心!然而,存放这一无价柳光的远古宝库却拒绝开启!

奥里大吃一惊,但森林之声向他解释道:狡猾的戈尔雷克人已为宝库增设了防护机制。

戈尔雷克人极其钟爱“字符串扩展”操作,同时也非常热衷于最长递增子序列(LIS)。

给定一个字符串 s1s2s3…sns_1s_2s_3 \ldots s_n,其“扩展”定义为如下字符串序列:
s1s_1, s1s2s_1 s_2, ..., s1s2…sns_1 s_2 \ldots s_n,
s2s_2, s2s3s_2 s_3, ..., s2s3…sns_2 s_3 \ldots s_n,
s3s_3, s3s4s_3 s_4, ...,
…\dots,
sn−1sns_{n-1} s_n,
sns_n。

例如,字符串 'abcd' 的“扩展”为以下字符串序列:
'a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd'。

要开启这座远古宝库,奥里必须求出字符串 ss 的“扩展”序列中最长递增子序列(LIS)的长度。此处字符串按字典序比较。

请帮助奥里完成这项任务!

字符串 aa 在字典序上严格小于字符串 bb,当且仅当满足以下任一条件:

  • aa 是 bb 的真前缀(即 aa 是 bb 的前缀,但 a≠ba \ne b);
  • 在 aa 与 bb 首次出现不同字符的位置上,aa 中该位置的字母在字母表中早于 bb 中对应位置的字母。

输入格式

Each test contains multiple test cases.

The first line contains one positive integer tt (1≤t≤1031 \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 nn (1≤n≤50001 \le n \le 5000) — length of the string.

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

It is guaranteed that the sum of nn over all test cases does not exceed 10410^4.

每个测试包含多个测试用例。

第一行包含一个正整数 tt(1≤t≤1031 \le t \le 10^3),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个正整数 nn(1≤n≤50001 \le n \le 5000)—— 字符串的长度。

每个测试用例的第二行包含一个长度为 nn 的非空字符串,该字符串仅由小写拉丁字母组成。

保证所有测试用例的 nn 值之和不超过 10410^4。

输出格式

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

对于每个测试用例,输出一个非负整数——即该问题的答案。

输入输出样例

  • 输入#1

    7
    5
    acbac
    8
    acabacba
    12
    aaaaaaaaaaaa
    10
    abacabadac
    8
    dcbaabcd
    3
    cba
    6
    sparky

    输出#1

    9
    17
    12
    29
    14
    3
    9

说明/提示

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'.

在第一个测试用例中,该字符串的“扩展”为:'a'、'ac'、'acb'、'acba'、'acbac'、'c'、'cb'、'cba'、'cbac'、'b'、'ba'、'bac'、'a'、'ac'、'c'。答案可以是,例如:'a'、'ac'、'acb'、'acba'、'acbac'、'b'、'ba'、'bac'、'c'。

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

首页