http://poj.org/problem?id=2406
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 50627 | Accepted: 21118 |
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.
Source
求最短循环节出现次数
1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 const int N(2333333); 8 int ans,len,p[N]; 9 char s[N]; 10 11 inline void Get_next() 12 { 13 for(int i=2,j=0;i<=len;i++) 14 { 15 for(;s[i]!=s[j+1]&&j>0;) j=p[j]; 16 if(s[i]==s[j+1]) j++; 17 p[i]=j; 18 } 19 } 20 21 int main() 22 { 23 for(scanf("%s",s+1);s[1]!=‘.‘;scanf("%s",s+1)) 24 { 25 memset(p,0,sizeof(p)); 26 len=strlen(s+1); 27 Get_next(); 28 int tmp=len-p[len]; 29 if(len%tmp) puts("1"); 30 else printf("%d\n",len/tmp); 31 } 32 return 0; 33 }
时间: 2024-10-23 02:12:06