G - Power Strings

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define MAXN 1000001
typedef long long LL;
/*
给定一个串,找最短循环节
*/
char s[MAXN];
int Next[MAXN];
void kmp_pre(int m)
{
    int j,k;
    j = 0;k = Next[0] = -1;
    while(j<m)
    {
        if(k==-1||s[j]==s[k])
            Next[++j] = ++k;
        else
            k = Next[k];
    }
}
int main()
{
    while(scanf("%s",s))
    {
        if(s[0]=='.') break;
        int l = strlen(s);
        kmp_pre(l);
        int ans = l - Next[l];
        if(l%ans==0)
            printf("%d\n",l/ans);
        else
            printf("1\n");
    }
}

 

posted @ 2017-04-06 18:32  joeylee97  阅读(120)  评论(0编辑  收藏  举报