At the time of learning KMP also did this problem, now it seems Rujia code to streamline some, after all, the shorter the better remember, the less prone to error.
and the code style of the recursive mismatch function of KMP and the code style of the Aho-corasick auto-mismatch function in the back are also consistent.
1#include <cstdio>2 3 Const intMAXN =1000000+Ten;4 CharS[MAXN];5 intF[MAXN];//a mismatch function6 7 intMain ()8 {9 //freopen ("In.txt", "R", stdin);Ten One intN, Kase =0; A while(SCANF ("%d", &n) = =1&&N) - { - GetChar (); the for(inti =0; I < n; i++) S[i] =GetChar (); - - //recursive solution of mismatch function -f[0] = f[1] =0; + for(inti =1; I < n; i++) - { + intj =F[i]; A while(j && s[i]! = s[j]) j =F[j]; atf[i+1] = s[i] = = S[j]? j+1:0; - } - -printf"Test Case #%d\n", ++Kase); - for(inti =2; I <= N; i++) - if(F[i] && i% (i-f[i]) = =0) inprintf"%d%d\n", I, I/(I-f[i])); -printf"\ n"); to } + - return 0; the}
code June
UVa 1328 (KMP for string cycles) Period