Describe
http://www.lydsy.com/JudgeOnline/problem.php?id=3670
For a string, find the num array, where num[i] represents the number of pairs in the same prefix that are not coincident in the string that consists of the first I character.
Analysis
When doing KMP self-match, by the way record a CNT array, where cnt[i] indicates that the I-character needs a few i=fail[i] to reach 0, that is, if the mismatch to jump to the I position, including I position, before the total number can be used as a mismatch jump object ( That is, how much satisfies the same prefix as a suffix). When a sub-match is made for the second time, the mismatch pointer jumps to a position of less than or equal to I/2 J, at which point Cnt[j] is the same prefix suffix group with the same number of satisfied num[i]. (Hand draw more clearly?)
P.s. If the I position jumps to K at the second match, then any number k after K ' is satisfied with K ' >i/2, then at I+1, K ' +1> (i+1)/2, so if you jump to K at position I, continue from i+1 position in k+1 position.
#include <cstdio>Const intmod=1e9+7;Const intmaxn=1e6+5;intans;CharC[MAXN];intFAIL[MAXN],CNT[MAXN];voidKMP () {intj=0; fail[1]=0, cnt[1]=1; for(intI=2; c[i];i++){ while(j&&c[j+1]!=c[i]) j=Fail[j]; if(c[i]==c[j+1]) J + +; Fail[i]=J; Cnt[i]=cnt[j]+1; } J=0; ans=1; for(intI=2; c[i];i++){ while(j&&c[j+1]!=c[i]) j=Fail[j]; if(c[i]==c[j+1]) J + +; while((j<<1) >i) j=Fail[j]; Ans=((Long Long) ans* (Long Long) (cnt[j]+1))%MoD; }}intMain () {intN; scanf ("%d",&N); while(n--) {scanf ("%s", c+1); KMP (); printf ("%d\n", ans); } return 0;}
View Code
Bzoj_3670_[noi2014]_ Zoo (KMP)