#P8368. [LNOI2022] 串

    ID: 9463 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>各省省选2022后缀自动机 SAMO2优化辽宁后缀数组 SA

[LNOI2022] 串

Problem Description

To help you better understand the statement, here are some definitions about strings:

  • For a string S=s1s2snS = s_1 s_2 \cdots s_n, its length is defined as S=n\lvert S \rvert = n.
  • For two strings S=s1s2snS = s_1 s_2 \cdots s_n and T=t1t2tmT = t_1 t_2 \cdots t_m, we call TT a substring of SS if m=0m = 0 (i.e., TT is the empty string) or 1ijn\exists 1 \le i \le j \le n such that T=sisi+1sjT = s_i s_{i + 1} \cdots s_j. If m=0m = 0 or in the above condition ii can be 11, then TT is called a prefix of SS; if m=0m = 0 or in the above condition jj can be nn, then TT is called a suffix of SS.

Given a string SS consisting of lowercase English letters, you need to find a string sequence (T0,T1,,Tl)(T_0, T_1, \ldots, T_l) that is as long as possible, satisfying:

  • T0T_0 is a substring of SS.
  • 1il\forall 1 \le i \le l, TiTi1=1\lvert T_i \rvert - \lvert T_{i - 1} \rvert = 1.
  • 1il\forall 1 \le i \le l, there exists a substring SiS'_i of SS with length Ti+1\lvert T_i \rvert + 1, such that the prefix of SiS'_i with length Ti1\lvert T_{i - 1} \rvert is Ti1T_{i - 1}, and the suffix of SiS'_i with length Ti\lvert T_i \rvert is TiT_i.

Output the maximum possible length of such a sequence (i.e., the maximum value of ll).

Input Format

This problem has multiple test cases. The first line contains an integer TT, denoting the number of test cases. For each test case, one line contains a string SS consisting of lowercase English letters.

Output Format

For each test case, output one integer per line, representing the maximum length of the string sequence described in the statement.

3
abcd
abab
a

2
3
0

Hint

[Sample Explanation #1]

In the following, the symbol ϵ\epsilon denotes the empty string.

For the first test case, we can find the following string sequence: $T_0 = \epsilon, T_1 = \texttt{b}, T_2 = \texttt{cd}$, where S1=ab,S2=bcdS'_1 = \texttt{ab}, S'_2 = \texttt{bcd}.

For the second test case, we can find the following string sequence: $T_0 = \epsilon, T_1 = \texttt{b}, T_2 = \texttt{ab}, T_3 = \texttt{bab}$, where $S'_1 = \texttt{ab}, S'_2 = \texttt{bab}, S'_3 = \texttt{abab}$.

For the third test case, we can find the following string sequence: T0=ϵT_0 = \epsilon.

[Sample #2]

See the attachments string/string2.in and string/string2.ans.

The string lengths in this sample have some gradients. You can use this sample to check your program.

[Sample #3]

See the attachments string/string3.in and string/string3.ans.

This sample satisfies Special Property A.

[Constraints]

Let S\sum |S| denote the sum of string lengths of all test cases in a test.

For 100%100 \% of the testdata, T1T \ge 1, 1S5×1051 \le \lvert S \rvert \le 5 \times {10}^5, 1S1.5×1061 \le \sum \lvert S \rvert \le 1.5 \times {10}^6.

Test Point ID S\lvert S \rvert \le S\sum \lvert S \rvert \le Special Property
121 \sim 2 3030 150150 None
353 \sim 5 200200 800800
686 \sim 8 10001000 30003000
9119 \sim 11 5×1055 \times {10}^5 1.5×1061.5 \times {10}^6 A
121512 \sim 15 6×1046 \times {10}^4 3×1053 \times {10}^5 None
162016 \sim 20 5×1055 \times {10}^5 1.5×1061.5 \times {10}^6

Special Property A: each character in the string is generated independently and uniformly at random among lowercase letters.

[Notes]

The input and output size of this problem is large, so please use faster I/O.

For example, if your code uses cin and cout for I/O, you may add the following statements after the I/O redirection statements (freopen, fopen, etc.) to speed up I/O.

ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

After adding these statements, it is not recommended to use cin, cout together with other I/O methods.

Translated by ChatGPT 5