E - Repetitive Structure of a String Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466 点

問題文

高橋君は文字列の構造を分析するプログラムを作っています。

文字列 S のボーダーとは、以下の条件をすべて満たす文字列 T のことを言います。

  • T は空文字列ではない。
  • T の長さは S の長さより真に小さい(すなわち |T| < |S|)。
  • T は S の接頭辞かつ接尾辞である。すなわち、S の先頭 |T| 文字が T と一致し、かつ S の末尾 |T| 文字も T と一致する。

例えば、文字列 abcab のボーダーは ab のみです。ab は abcab の先頭 2 文字とも末尾 2 文字とも一致し、空でなく、長さも abcab より真に小さいからです。一方、a は先頭 1 文字とは一致しますが末尾 1 文字(b)とは一致しないため、ボーダーではありません。

別の例として、文字列 abababab のボーダーは ab、abab、ababab の 3 つで、それぞれの長さは 2, 4, 6 です。

なお、長さ |S| 未満の正整数 \ell を決めると、S の先頭 \ell 文字からなる文字列は一意に定まり、それが S の末尾 \ell 文字と一致するかも一意に決まります。したがって、同じ長さのボーダーは高々 1 つであり、ボーダーの集合はその長さの集合と一対一に対応します。

高橋君は Q 個の文字列を受け取り、それぞれについて以下の値を求めたいと考えています。

文字列 S のボーダーの個数を k とします。k \geq 1 のとき、すべてのボーダーの長さを昇順に並べた列を d_1 < d_2 < \dots < d_k とします(ボーダーの長さはすべて異なるため、この列は狭義単調増加です)。このとき、次の値を求めてください。

  • k = 0 の場合(ボーダーが存在しない):0 を出力する。
  • k = 1 の場合(ボーダーがちょうど 1 つ):そのボーダーの長さ d_1 を出力する。
  • k \geq 2 の場合(ボーダーが 2 つ以上):隣接する要素の差 d_2 - d_1,\, d_3 - d_2,\, \dots,\, d_k - d_{k-1} はいずれも正の整数である。これら k-1 個の正整数の最大公約数を出力する。

先ほどの例では、abababab のボーダーの長さは d_1 = 2,\, d_2 = 4,\, d_3 = 6 なので、隣接要素の差は d_2 - d_1 = 2,\, d_3 - d_2 = 2 となり、その最大公約数は 2 です。

制約

  • 1 \leq Q \leq 10^5
  • 各文字列 S_i の長さ |S_i| は 1 \leq |S_i| \leq 10^6 を満たす。
  • S_i は英小文字からなる。
  • すべての文字列の長さの合計は \displaystyle \sum_{i=1}^{Q} |S_i| \leq 10^6 を満たす。

入力

Q
S_1
S_2
\vdots
S_Q
  • 1 行目には、文字列の個数を表す整数 Q が与えられる。
  • 続く Q 行にわたって、各文字列が 1 行に 1 つずつ与えられる。
  • 1 + i 行目には、i 番目の文字列 S_i が与えられる。各 S_i は英小文字からなる。

出力

Q 行にわたって出力せよ。i 行目(1 \leq i \leq Q)には、文字列 S_i に対する答えを整数 1 つで出力せよ。


入力例 1

5
abcab
abababab
aaaa
abcde
aba

出力例 1

2
2
1
0
1

入力例 2

6
a
aa
ab
abcabc
zzz
xyxxyx

出力例 2

0
1
0
3
1
2

入力例 3

8
abcababcab
aaaaabaaaaab
abababa
abcabcabcabc
abcdabcabcd
zzxyzzxyzz
mississippi
aabaaabaaab

出力例 3

3
6
2
3
4
1
0
4

入力例 4

10
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
abababababababababababababababababababababababababababababababab
abcabcabcabcabcabcabcabcabcabcabcabcabcabc
abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd
thequickbrownfoxjumpsoverthelazydogthequickbrownfox
zzzzzyzzzzzyzzzzzyzzzzzyzzzzzy
aabbaabbaabbaabbaabbaabbaabbaabb
abcdeffedcbaabcdeffedcbaabcdeffedcba
mnopqrmnopqrmnopqrmnopqrmnopqr

出力例 4

26
1
2
3
4
16
6
4
1
6

入力例 5

1
a

出力例 5

0

Score : 466 pts

Problem Statement

Takahashi is writing a program to analyze the structure of strings.

A border of a string S is a string T that satisfies all of the following conditions:

  • T is not the empty string.
  • The length of T is strictly less than the length of S (i.e., |T| < |S|).
  • T is both a prefix and a suffix of S. That is, the first |T| characters of S match T, and the last |T| characters of S also match T.

For example, the only border of the string abcab is ab. This is because ab matches both the first 2 characters and the last 2 characters of abcab, is non-empty, and has length strictly less than that of abcab. On the other hand, a matches the first 1 character but does not match the last 1 character (b), so it is not a border.

As another example, the string abababab has 3 borders: ab, abab, and ababab, with lengths 2, 4, 6 respectively.

Note that for a positive integer \ell less than |S|, the string consisting of the first \ell characters of S is uniquely determined, and whether it matches the last \ell characters of S is also uniquely determined. Therefore, there is at most one border of each length, and the set of borders is in one-to-one correspondence with the set of their lengths.

Takahashi receives Q strings and wants to compute the following value for each of them.

Let k be the number of borders of string S. When k \geq 1, let d_1 < d_2 < \dots < d_k be the sequence of all border lengths sorted in ascending order (since all border lengths are distinct, this sequence is strictly increasing). Compute the following value:

  • If k = 0 (no border exists): output 0.
  • If k = 1 (exactly one border): output the length of that border, d_1.
  • If k \geq 2 (two or more borders): the differences between adjacent elements d_2 - d_1,\, d_3 - d_2,\, \dots,\, d_k - d_{k-1} are all positive integers. Output the greatest common divisor of these k-1 positive integers.

In the example above, the border lengths of abababab are d_1 = 2,\, d_2 = 4,\, d_3 = 6, so the differences between adjacent elements are d_2 - d_1 = 2,\, d_3 - d_2 = 2, and their greatest common divisor is 2.

Constraints

  • 1 \leq Q \leq 10^5
  • The length |S_i| of each string S_i satisfies 1 \leq |S_i| \leq 10^6.
  • S_i consists of lowercase English letters.
  • The total length of all strings satisfies \displaystyle \sum_{i=1}^{Q} |S_i| \leq 10^6.

Input

Q
S_1
S_2
\vdots
S_Q
  • The first line contains an integer Q representing the number of strings.
  • The following Q lines each contain one string.
  • The (1 + i)-th line contains the i-th string S_i. Each S_i consists of lowercase English letters.

Output

Output Q lines. The i-th line (1 \leq i \leq Q) should contain a single integer representing the answer for string S_i.


Sample Input 1

5
abcab
abababab
aaaa
abcde
aba

Sample Output 1

2
2
1
0
1

Sample Input 2

6
a
aa
ab
abcabc
zzz
xyxxyx

Sample Output 2

0
1
0
3
1
2

Sample Input 3

8
abcababcab
aaaaabaaaaab
abababa
abcabcabcabc
abcdabcabcd
zzxyzzxyzz
mississippi
aabaaabaaab

Sample Output 3

3
6
2
3
4
1
0
4

Sample Input 4

10
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
abababababababababababababababababababababababababababababababab
abcabcabcabcabcabcabcabcabcabcabcabcabcabc
abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd
thequickbrownfoxjumpsoverthelazydogthequickbrownfox
zzzzzyzzzzzyzzzzzyzzzzzyzzzzzy
aabbaabbaabbaabbaabbaabbaabbaabb
abcdeffedcbaabcdeffedcbaabcdeffedcba
mnopqrmnopqrmnopqrmnopqrmnopqr

Sample Output 4

26
1
2
3
4
16
6
4
1
6

Sample Input 5

1
a

Sample Output 5

0