Power Strings
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
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).
输入
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.
输出
For each s you should print the largest n such that s = a^n for some string a.
示例输入
abcdaaaa ababab .
示例输出
14 3
提示
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
来源
示例程序
1. #include<stdio.h>
2. #include<string.h>
3. #define h 1000001
4. char str1[h];
5. int next[h];
6. void getnext(char str[h])
7. {
8. int i = 0;
9. int len = strlen(str);
10. next[0] = -1;
11. int j = -1;
12. while(i < len)
13. {
14. if( j == -1 || str[i] == str[j])
15. {
16. ++i;
17. ++j;
18. if( str[i]!= str[j])
19. next[i] = j;
20. else
21. next[i] = next[j];
22. }
23. else
24. j = next[j];
25. }
26. }
27. int main()
28. {
29. int n,i,len;
30. while(scanf("%s",str1)!=EOF && str1[0]!='.')
31. {
32. getnext(str1);
33. len = strlen(str1);
34. if(len%(i=len-next[len])==0)
35. {
36. n=len/i;
37. }
38. else
39. n=1;
40. "%d\n",n);
41. }
42. return 0;
43. }