#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace STD;voidGetfail (Char*p,int*F) {intm=strlen(p); f[0]=0; f[1]=0; for(intI=1; i<m; i++) {intJ=f[i]; while(J&&p[i]!=p[j]) j=f[j]; f[i+1]=p[i]==p[j]?j+1:0; }}intKmpChar*t,Char*p,int*F) {intCnt=0, i,j,n=strlen(t), m=strlen(p); Getfail (P,F); for(i=0, j=0; i<n; i++) { while(J&&p[j]!=t[i]) j=f[j];if(P[j]==t[i]) j + +;if(j==m) {cnt++; j=0; } }returnCNT;}intMain () {Chart[1024x768],p[1024x768];intf[1024x768]; while(~scanf('%s ', T)) {if(strcmp(T,"#")==0) Break;scanf('%s ', p);printf("%d\n", KMP (t,p,f)); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu 2087 Cut Flower strips