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.
示例输入
abcd aaaa ababab .
示例输出
1 4 3
提示
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
来源
示例程序
- #include<stdio.h>
- #include<string.h>
- #define h 1000001
- char str1[h];
- int next[h];
- void getnext(char str[h])
- {
- int i = 0;
- int len = strlen(str);
- next[0] = -1;
- int j = -1;
- while(i < len)
- {
- if( j == -1 || str[i] == str[j])
- {
- ++i;
- ++j;
- if( str[i]!= str[j])
- next[i] = j;
- else
- next[i] = next[j];
- }
- else
- j = next[j];
- }
- }
- int main()
- {
- int n,i,len;
- while(scanf("%s",str1)!=EOF && str1[0]!='.')
- {
- getnext(str1);
- len = strlen(str1);
- if(len%(i=len-next[len])==0)
- {
- n=len/i;
- }
- else
- n=1;
- printf("%d\n",n);
- }
- return 0;
- }