String Exponentiation

Time limit1sMemory limit256 MB

Summary
Given a string, find the largest exponent n such that the string is a repetition of some base string a raised to n.
Level

Medium5 of 10

Topics
String, String matching, Number theory
Solved
No attempts yet

Problem

For two strings aa and bb made of lowercase letters, a∗ba * b denotes their concatenation. For example, if a=abca=\texttt{abc} and b=defb=\texttt{def}, then a∗b=abcdefa * b=\texttt{abcdef}.

If we treat this concatenation as multiplication, we can also define exponentiation by a non-negative integer:

  • a0=εa^0 = \varepsilon (the empty string)
  • an+1=a∗ana^{n+1} = a * a^n

Given a string ss, write a program that finds the largest nn for which some string aa satisfies s=ans = a^n.

Input

The input consists of at most 10 test cases. Each test case is a single line containing the string ss. The length of ss is at least 1 and at most 1,000,000. The line following the last test case contains a single period (.).

Output

For each test case, print on its own line the largest nn such that s=ans = a^n.

Examples6

  1. Example 1

    Input
    abcd
    aaaa
    ababab
    .
    
    Expected output
    1
    4
    3
    
  2. Example 2

    Input
    a
    .
    
    Expected output
    1
    
  3. Example 3

    Input
    zzzzzzz
    z
    .
    
    Expected output
    7
    1
    
  4. Example 4

    Input
    abcabcab
    abcabcabc
    .
    
    Expected output
    1
    3
    
  5. Example 5

    Input
    abababababab
    abab
    .
    
    Expected output
    6
    2
    
  6. Example 6

    Input
    abababa
    aabaab
    .
    
    Expected output
    1
    2