Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
标准的KMP 贴个标准的代码给忘了的时候看#include <cstdio> #include <memory.h> using namespace std; char text[1000005]; char pattern[10005]; int mnext[10005]; void getNext(char *s,int len) { memset(mnext,0,sizeof(mnext)); mnext[0]=-1; int i=0; int j=-1; while(i<=len-1) { if(j==-1||s[i]==s[j]) { i++; j++; mnext[i]=j; } else j=mnext[j]; } } int kmpCount(char *da,char* xiao,int tlen,int plen) { int i=0; int j=0; int result=0; while(i<=tlen-1) { if(j==-1||da[i]==xiao[j]) { i++; j++; } else j=mnext[j]; if(j==plen) result++; } return result; } int main() { int rounds; scanf("%d",&rounds); while(rounds--) { scanf("%s",pattern); scanf("%s",text); int tlen=0; int plen=0; for(;text[tlen]!='\0';tlen++); for(;pattern[plen]!='\0';plen++); getNext(pattern,plen); int result=kmpCount(text,pattern,tlen,plen); printf("%d\n",result); memset(pattern,0,sizeof(pattern)); memset(text,0,sizeof(text)); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator