Test instructions
The text string can be broken up into several pattern strings.
Analysis:
Kmp
#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<11#defineAll 1,n,1#defineN 1010#defineRead Freopen ("In.txt", "R", stdin)Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;CharT[n],p[n];intF[n];voidGetNextintN) { intI=0, j=-1; f[0]=-1; while(i<N) { if(j==-1|| p[i]==P[j]) {i++; J++; F[i]=J; } Elsej=F[j]; }}voidKMP () {intn=strlen (T); intm=strlen (P); GetNext (m); intnum=0, i=0, j=0; while(i<N) { if(t[i]==p[j]| | j==-1) {i++; J++; } Elsej=F[j]; if(j==m) {num++; J=0;//contains a pattern string, J 0 continue looking backwards}} printf ("%d\n", num);}intMain () { while(~SCANF ("%s", T)) { if(t[0]=='#') Break; scanf ("%s", P); KMP (); }return 0;}
Hdu 2087-Cut flower strips (KMP)