Poj 2406 Power Strings Cycle Problem, poj2406
Power Strings
Time Limit:3000 MS |
|
Memory Limit:65536 K |
Total Submissions:48139 |
|
Accepted:20040 |
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 shoshould print the largest n such that s = a ^ n for some string.
Sample Input
abcdaaaaababab.
Sample Output
143
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
Source
Waterloo local 2002.07.01 Baidu nonsense: Memory limit: Time Limit: 3000 ms 65536 k
Comments: Total 48139 20040 accepted:
Description
We give two strings A and B which define their concatenation *. For example, if A and B = "abc", then in "def" = "* (ABCDEF )". If we think of exponentiation concatenation reproduction, the non-negative integer is defined as: in the normal mode (0 = "" (Null String) and (N + 1) = (n × meters ).
Input
The input line of each test case represents a printable character, string ). The length of S will be at least 1 million characters and no more than 1. The cycle line is the final test case.
Output
You should print up to N, S = N such strings for each.
Sample Input
ABCD
AAAA
ABABAB
.
Sample output
1
4
3
Prompt
This problem has a huge investment, rather than using scanf's CIN, to avoid exceeding the time limit.
Source code
The period of the local 0, Waterloo hypothesis S is greater than 1. Assume that S has a period X, and each
The length of each cycle is L. So, we have S [1 .. n-L] = S [L + 1... N],
For I = n, n-L is a legal j.
* Because the P array asks for the maximum value of all j values, as long as P [n] is determined
Yes. If P [n] <n/2, S does not have a cycle; otherwise, the minimum cycle
The length is n-p [n], and the corresponding period is n/(n-p [n]).
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 char a[1000002]; 6 int p[1000002]; 7 void makep(int l) 8 { 9 memset(p,0,sizeof(p));10 int j=0;11 for(int i=1;i<l;i++)12 {13 while(a[i]!=a[j]&&j>0)14 j=p[j-1];15 if(a[i]==a[j])16 j++;17 p[i]=j;18 }19 }20 void deal(int l)21 {22 int ans=1;23 if(l%(l-p[l-1])==0)24 ans=l/(l-p[l-1]);25 printf("%d\n",ans);26 }27 int main()28 {29 while(scanf("%s",a)==1)30 {31 if(a[0]=='.')break;32 int l=strlen(a);33 makep(l);34 deal(l);35 }36 return 0;37 }