POJ——T 2406 Power Strings
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ——T 2406 Power Strings相关的知识,希望对你有一定的参考价值。
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 }
以上是关于POJ——T 2406 Power Strings的主要内容,如果未能解决你的问题,请参考以下文章