Power Strings
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 56357 | Accepted: 23429 |
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.
直接用kmp的next函数。
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 const int maxn=1000006; 5 char a[maxn]; 6 int next[maxn]; 7 int n; 8 9 void get_next(){ 10 int temp=-1,i=0; 11 next[i]=-1; 12 while( i!=n){ 13 if(temp==-1 || a[i]==a[temp]) 14 next[++i]=++temp; 15 else 16 temp=next[temp]; 17 } 18 } 19 20 int main() 21 { 22 while( ~scanf("%s",a)){ 23 if(a[0]=='.') break; 24 n=strlen(a); 25 get_next(); 26 // for(int i=0;i<=n;i++) 27 // printf("%d ",next[i]); 28 int length=n-next[n]; 29 if(length!=n&&n%length==0) 30 printf("%d ",n/length); 31 else 32 printf("1 "); 33 } 34 return 0; 35 }