Period
Test instructions: A string of n (2 <= n <= 1 000 000), asking if the prefix string length is K (k > 1) is a periodic string, that is, k = a ... A; If you press K from small to large, the output k is the number of cycles;
Sample Input3 AAAAabaabaabaab0 Sample OutputTest Case #12 23 3 Test Case #22 26 29 34the title is actually from LA. Very good a problem, with the original KMP.write the understanding of KMP:the KMP written here is directly starting from 0, and the 0,1 mismatch points to 0;KMP Getfail () is actually using the currently determined p[i] = P[[f[i]] to give i+1 a matching opportunity (special attention is not equal) that is f[i+1] = j+ 1; This is the subscript transfer of the mismatch edge, when the i+1, just find and p[i] equal d[f[j]], but did not change the original matching relationship, which is just to push down;there is a need to note that the KMP actually matched to the first Len, this one is originally ' ", but because of the recursion in the next one of the relationship, this bit is actually matched; This problem can be used to match the first Len ~ ~ Very good idea: How to know that a string with a prefix length of 2 is a period string, such as AA? Then we need to move back to 1, how do we know to think about it? This is the meaning of the mismatch edge of f[i] ~ ~ ~ because if p[0] = p[1] = ' A ', then p[2] mismatch, obviously simple kmp] = 1; this is the cycle; and that's why the following is from 2 onwards to N;also I represents the length of the prefix, i.e. K (i-f[i]) = i, (k > 1) so f[i] > 0 and (i-f[i]) |i;
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Const intN = 1e6 +7;CharP[n];intF[n];voidGetfail (Char*p,int*f) {f[0] = f[1] =0; intn =strlen (P); for(inti =1; I < n;i++){ intj =F[i]; if(j && p[i]! = p[j]) j =F[j]; F[i+1] = (P[i] = = P[j]? j+1:0);//i+1 will be pushed to nth bit }}intMain () {intN,kase =1; while(SCANF ("%d", &n) = =1&&N) {scanf ("%s", p); Getfail (P,F); printf ("Test Case #%d\n", kase++); for(inti =2; I <= n;i++) {//i = n * * if(F[i] >0&& i% (i-f[i]) = =0) printf ("%d%d\n", i,i/(i-f[i])); } puts (""); }}
HDU 1358 period KMP Getting Started