Description
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
找最小循环节
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[1000005];
int nxt[1000005];
int cnt;
void get_nxt()
{
int i=0;
int j=-1;
nxt[0]=-1;
int l=strlen(a);
while(i<l)
{
if(j==-1||a[i]==a[j])
{
i++;
j++;
nxt[i]=j;
}
else
j=nxt[j];
}
}
int main()
{
int i,j,k,m,n,l;
while(~scanf("%s",a),a[0]!='.')
{
int f=0;
memset(nxt,0,sizeof(nxt));
int l1=strlen(a);
get_nxt();
i=l1;
int t=i-nxt[i];
if(i!=t&&i%t==0)
{
f=1;
printf("%d\n",l1/t);
}
if(f==0)
printf("1\n");
}
return 0;
}