Test instructions: give you two strings p and s to find out how many times p appears in S.
I want to get p directly to the front of S, then the next array to strlen (p) Take the remainder ==0 can, after WA. Finally found that a aassaas when there is a bug, only to think of P and s in the middle of an impossible character ' $ ' on it, the ring is a.
#include <bits/stdc++.h>using namespacestd;Const intinf=0x3f3f3f3f; typedefLong Longll;#definePI (a) printf ("%d\n", a)#defineSI (n) scanf ("%d",& (n))#defineSII (n,m) scanf ("%d%d",& (N),& (M))#defineCLE (A,val) memset (A, (Val), sizeof (a))#defineRep (i,b) for (int i=0;i< (b); i++)#defineREP (I,A,B) for (int i= (a); i<= (b); i++)#defineREReP (I,A,B) for (int i= (a); i>= (b); i--)Const Doubleeps= 1e-9 ;/*/////////////////////////c o d i n g S p a C e/////////////////////////*/Const intmaxn=10000+1000000+9 ;voidKmp_pre (intX[],intMintnext[]) { inti,j; J=next[0]=-1; I=0; while(i<m) { while(-1!=J&&X[I]!=X[J]) j=Next[j]; next[++i]=++J; }}intNEXT[MAXN],INP[MAXN];intn,m;Charstr1[10000+5],str2[1000000+5];intMain () {into; SI (o); while(o--) {scanf ("%s", STR1); scanf ("%s", STR2); N=strlen (str1), m=strlen (STR2); Str1[n]='$'; N++; for(intI=0; i<n;i++) Inp[i]=Str1[i]; for(inti=n;i<n+m;i++) Inp[i]=str2[i-N]; Kmp_pre (Inp,n+m,next); intans=0; Rep (i,1, n+m)if(next[i]% (n1)==0&&next[i]!=0) ans++; PI (ANS); } return 0;}
POJ-3461 Oulipo (KMP, number of pattern strings appearing in the main string)