Power Strings
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
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 const int maxn=1000005; 6 7 int next[maxn]; 8 9 void getnext(char str[]) 10 { 11 int i,j; 12 int len=strlen(str); 13 i=0;j=-1; 14 next[i]=j; 15 while(i<=len) 16 { 17 if(j==-1||str[i]==str[j]) 18 { 19 i++; 20 j++; 21 next[i]=j; 22 } 23 else 24 j=next[j]; 25 } 26 } 27 28 int main() 29 { 30 //freopen("in.txt","r",stdin); 31 int cnt,i; 32 char s[maxn]; 33 while(scanf("%s",s)&&strcmp(s,".")!=0) 34 { 35 getnext(s); 36 int len=strlen(s); 37 if(len%(len-next[len])==0) 38 printf("%d ",len/(len-next[len])); 39 else 40 printf("%d ",1); 41 } 42 return 0; 43 }