NOIP '20 P2 - String Matching

View as PDF

Submit solution

Points: 12 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem type

Little C has just finished learning about string matching, and he is practicing now.

For a string S, the question asks him to find the number of ways to split S in the following form:

S=ABC, S=ABABC, S=ABABABC, where A, B, C are all non-empty strings. The number of characters appearing an odd number of times in A will not be more than the number of characters appearing an odd number of times in C.

More specifically, we can define AB to represent the concatenation of two strings A and B. For example A=aab, B=ab, then AB=aabab.

We also recursively define A1=A, An=An1A (n2 and is a positive integer). For example, A=abb, then A3=abbabbabb.

Little C's problem is asking to find the number of ways of S=(AB)iC, where F(A)F(C); F(S) represents the number of characters that appear an odd number of times in S. Two ways are considered different if and only if at least one string in the split A, B and C is different.

Little C doesn't know how to solve this problem, so he asked you for help.

Input Specification

A positive integer T in the first line of the input file represents the number of data sets in the input.

The next T lines contain a string S for each data set on each line. S consisting of lowercase letters only.

Output Specification

For each data set, output a line with one integer indicating the answer.

Sample Input 1

Copy
3
nnrnnr
zzzaab
mmlmmlo

Sample Output 1

Copy
8
9
16

Explanation for Sample 1

All possible ways are

  1. A=n, B=nr, C=nnr.
  2. A=n, B=nrn, C=nr.
  3. A=n, B=nrnn, C=r.
  4. A=nn, B=r, C=nnr.
  5. A=nn, B=rn, C=nr.
  6. A=nn, B=rnn, C=r.
  7. A=nnr, B=n, C=nr.
  8. A=nnr, B=nn, C=r.

Sample Input 2

Copy
5
kkkkkkkkkkkkkkkkkkkk
lllllllllllllrrlllrr
cccccccccccccxcxxxcc
ccccccccccccccaababa
ggggggggggggggbaabab

Sample Output 2

Copy
156
138
138
147
194

Additional Samples

Additional samples can be found here.

  • Sample 3 (string3.in and string3.ans).
  • Sample 4 (string4.in and string4.ans).

Constraints

Test Case |S| Additional Constraints
14 10 None
58 100 None
912 1000 None
1314 215 S contains only one character
1517 216 S contains only two characters
1821 217 None
2225 220 None

For all test cases, 1T5, 1|S|220.

Problem translated to English by Tommy_Shan.


Comments

There are no comments at the moment.