#P7114. [NOIP2020] 字符串匹配

    ID: 8053 远端评测题 1000ms 512MiB 尝试: 9 已通过: 6 难度: 7 上传者: 标签>字符串2020倍增NOIP 提高组哈希 hashing

[NOIP2020] 字符串匹配

Problem Description

After learning about string matching, Little C is now working on a problem.

For a string SS, the task asks him to find the number of all split plans of SS that have one of the following forms:

S=ABCS = ABC, S=ABABCS = ABABC, S=ABABABCS = ABAB \ldots ABC, where AA, BB, and CC are all non-empty strings, and the number of characters that appear an odd number of times in AA is not greater than the number of characters that appear an odd number of times in CC.

More specifically, we define ABAB as the concatenation of two strings AA and BB. For example, if A=aabA = \texttt{aab} and B=abB = \texttt{ab}, then AB=aababAB = \texttt{aabab}.

We also define recursively A1=AA^1 = A and An=An1AA^n = A^{n - 1} A (n2n \ge 2 and nn is a positive integer). For example, if A=abbA = \texttt{abb}, then A3=abbabbabbA^3 = \texttt{abbabbabb}.

Then Little C's exercise is to find the number of plans such that S=(AB)iCS = {(AB)}^i C, where F(A)F(C)F(A) \le F(C). Here, F(S)F(S) denotes the number of characters that appear an odd number of times in the string SS. Two plans are different if and only if at least one of the split strings AA, BB, and CC is different.

Little C cannot solve this problem, so he asks you for help. Please help him.

Input Format

This problem contains multiple test cases. The first line of the input file contains a positive integer TT, indicating the number of test cases.

Each test case contains one line with a string SS, as described above. SS consists only of lowercase English letters.

Output Format

For each test case, output one integer per line, representing the answer.

3
nnrnnr
zzzaab
mmlmmlo

8
9
16

5
kkkkkkkkkkkkkkkkkkkk
lllllllllllllrrlllrr
cccccccccccccxcxxxcc
ccccccccccccccaababa
ggggggggggggggbaabab

156
138
138
147
194

见附件中的 string/string3.in
见附件中的 string/string3.ans
见附件中的 string/string4.in
见附件中的 string/string4.ans

Hint

【Sample #1 Explanation】

For the first test case, all valid plans are:

  1. A=nA = \texttt{n}, B=nrB = \texttt{nr}, C=nnrC = \texttt{nnr}.
  2. A=nA = \texttt{n}, B=nrnB = \texttt{nrn}, C=nrC = \texttt{nr}.
  3. A=nA = \texttt{n}, B=nrnnB = \texttt{nrnn}, C=rC = \texttt{r}.
  4. A=nnA = \texttt{nn}, B=rB = \texttt{r}, C=nnrC = \texttt{nnr}.
  5. A=nnA = \texttt{nn}, B=rnB = \texttt{rn}, C=nrC = \texttt{nr}.
  6. A=nnA = \texttt{nn}, B=rnnB = \texttt{rnn}, C=rC = \texttt{r}.
  7. A=nnrA = \texttt{nnr}, B=nB = \texttt{n}, C=nrC = \texttt{nr}.
  8. A=nnrA = \texttt{nnr}, B=nnB = \texttt{nn}, C=rC = \texttt{r}.

【Constraints】

Test Point ID S\lvert S \rvert \le Special Property
141 \sim 4 1010 None
585 \sim 8 100100
9129 \sim 12 10001000
131413 \sim 14 2152^{15} SS contains only one kind of character
151715 \sim 17 2162^{16} SS contains only two kinds of characters
182118 \sim 21 2172^{17} None
222522 \sim 25 2202^{20}

For all test points, it is guaranteed that 1T51 \le T \le 5 and 1S2201 \le |S| \le 2^{20}.

Translated by ChatGPT 5