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.
i-next[i]:表示已i结尾的前缀的循环节的长度
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char s[1000005];
int nex[1000005],len;
void get_next();
int main()
{
int a;
while(scanf("%s",s)!=EOF)
{
if(s[0]=='.')
break;
get_next();
a=len-nex[len-1]; //循环节长度
if(len%a==0)
printf("%d\n",len/a);
else
printf("1\n");
}
return 0;
}
void get_next()
{
int i,j;
len=strlen(s);
i=0;
j=0;
nex[0]=0;
for(i=1;i<len;i++)
{
j=nex[i-1];
while(j!=0&&s[i]!=s[j])
{
j=nex[j-1];
}
if(s[i]==s[j])
nex[i]=j+1;
else
nex[i]=0;
}
}