#P1117. [NOI2016] 优秀的拆分

    ID: 117 远端评测题 1500ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>字符串2016NOI 系列O2优化枚举,暴力后缀数组,SA

[NOI2016] 优秀的拆分

Description

If a string can be split into the form AABB\text{AABB}, where A\text{A} and B\text{B} are any non-empty strings, then we call this split of the string excellent. For example, for the string aabaabaa\texttt{aabaabaa}, if we set A=aab\text{A}=\texttt{aab} and B=a\text{B}=\texttt{a}, we have found one way to split this string into AABB\text{AABB}.

A string may have no excellent split, or it may have more than one excellent split. For example, if we set A=a\text{A}=\texttt{a} and B=baa\text{B}=\texttt{baa}, the above string can also be expressed as AABB\text{AABB}; however, the string abaabaa\texttt{abaabaa} has no excellent split.

Now given a string SS of length nn, we need to count, over all substrings of SS and all their possible splits, the total number of excellent splits. Here a substring refers to a contiguous segment of the string.

Notes:

  1. Identical substrings that appear at different positions are considered different substrings, and their excellent splits are all counted in the answer.
  2. In a split, A=B\text{A}=\text{B} is allowed. For example, cccc\texttt{cccc} has the split A=B=c\text{A}=\text{B}=\texttt{c}.
  3. The string itself is also one of its substrings.

Input Format

Each input file contains multiple test cases.

The first line of the input file contains a single integer TT, indicating the number of test cases.

Then follow TT lines, each containing a string SS consisting only of lowercase English letters, as described above.

Output Format

Output TT lines. Each line contains an integer, indicating the total number of excellent splits among all splits of all substrings of SS.

4
aabbbb
cccccc
aabaabaabaa
bbaabaababaaba

3
5
4
7

Hint

Sample explanation.

We use S[i,j]S[i, j] to denote the substring of SS from the ii-th character to the jj-th character (1-based indexing).

In the first dataset, there are three substrings with excellent splits: S[1,4]=aabbS[1,4]=\texttt{aabb}, whose excellent split is A=a\text{A}=\texttt{a}, B=b\text{B}=\texttt{b}; S[3,6]=bbbbS[3,6]=\texttt{bbbb}, whose excellent split is A=b\text{A}=\texttt{b}, B=b\text{B}=\texttt{b}; S[1,6]=aabbbbS[1,6]=\texttt{aabbbb}, whose excellent split is A=a\text{A}=\texttt{a}, B=bb\text{B}=\texttt{bb}. The remaining substrings have no excellent splits, so the answer for the first dataset is 33.

In the second dataset, there are in total four substrings that have excellent splits, divided into two types: for the substrings S[1,4]=S[2,5]=S[3,6]=ccccS[1,4]=S[2,5]=S[3,6]=\texttt{cccc}, they share the same excellent split A=c\text{A}=\texttt{c}, B=c\text{B}=\texttt{c}, but since these substrings are at different positions, they are counted three times; for the substring S[1,6]=ccccccS[1,6]=\texttt{cccccc}, it has two excellent splits: A=c\text{A}=\texttt{c}, B=cc\text{B}=\texttt{cc} and A=cc\text{A}=\texttt{cc}, B=c\text{B}=\texttt{c}, which are different splits of the same substring and are both counted. Therefore, the answer for the second dataset is 3+2=53+2=5.

In the third dataset, S[1,8]S[1,8] and S[4,11]S[4,11] each have two excellent splits. Among them, S[1,8]S[1,8] is the example in the problem statement, so the answer is 2+2=42+2=4.

In the fourth dataset, S[1,4]S[1,4], S[6,11]S[6,11], S[7,12]S[7,12], S[2,11]S[2,11], and S[1,8]S[1,8] each have one excellent split, while S[3,14]S[3,14] has two excellent splits, so the answer is 5+2=75+2=7.

Constraints

For all test points, it is guaranteed that 1T101 \leq T \leq 10. The following limits apply to a single input within a dataset; that is, within the same test point, all of its TT datasets satisfy the constraints.

Let nn be the length of string SS. The detailed constraints for each test point are shown in the table below:

::cute-table{tuack}

Test point ID nn \leq Special property
121 \sim 2 300300 All characters in SS are the same
343 \sim 4 20002\,000 ^
565 \sim 6 1010 None
787 \sim 8 2020 ^
9109 \sim 10 3030
111211 \sim 12 5050
131413 \sim 14 100100
1515 200200
1616 300300
1717 500500
1818 10001\,000
1919 20002\,000
2020 3000030\,000

Translated by ChatGPT 5