Link:
Http://acm.hust.edu.cn/problem.php? Id = 1010
Question:
Description
There is a string. the length of a is less than 1,000,000. I rewrite it again and again. then I got a new string: aaaaaa ...... now I cut it from two different position and get a new string B. then,
Give you the string B, can you tell me the length of the shortest possible string.
For example, a = "abcdefg". I got ABCDEfgabcdefgabcdeFgabcdefg... then I cut the red part: efgabcdefgabcde as string B. From B, you shoshould find out the shortest.
Input
Multiply test cases.
For each line there is a string B which contains only lowercase and uppercase charactors.
The length of B is no more than 1,000,000.
Output
For each line, output an integer, as described above.
Sample Input
bcabcabefgabcdefgabcde
Sample output
37
Question:
There is a string a. Assume that A is "abcdefg", and a can repeat aaaaaaa, that is, "abcdefgabcdefgabcdefg .....".
Extract an "ABCD" section from it.EfgabcdefgabcdeFgabcdefg ", take the red part as the cut part, and set it as string B.
Now, the string B is given first, and the shortest length of A is obtained.
Analysis and Summary:
Set the string c = aaaaaaaa .... since C is composed of countless A, so there are no number of loops in A, from any starting point in C, there can also be a loop, and the cycle length is the same as that of the original. (Like a circle, it can go back to the origin from any point ).
Therefore, string B is treated as a string starting from B [0]. The original problem can be converted to: Find the shortest cycle node of string B.
Based on the minimum cyclic Node Method, you can easily obtain this question.
Code:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MAXN = 1000005;char T[MAXN];int f[MAXN];void getFail(char *p,int *f){ int n=strlen(p); f[0]=f[1]=0; for(int i=1; i<n; ++i){ int j=f[i]; while(j && p[i]!=p[j]) j=f[j]; f[i+1] = p[i]==p[j]?1+j:0; }}int main(){ while(gets(T)){ getFail(T,f); int n=strlen(T); printf("%d\n", n-f[n]); } return 0;}
-- The significance of life is to give it a meaningful person.
Original Http://blog.csdn.net/shuangde800,
D_double (reprinted please mark)