| ||||||||||
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 |
Re:第一次KMP AC!分享下TLE教训.In Reply To:Re:第一次KMP AC!分享下TLE教训. Posted by:zhoutl at 2013-07-29 21:51:29 #include<stdio.h> #include<vector> #include<map> #include<set> #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cmath> using namespace std; int *qiu_next(char *str) { int len=strlen(str); int *next=new int[len]; next[0]=-1; int j=0; int k=-1; while(j<len-1) { if(k<0||str[k]==str[j]) { k++;j++; next[j]=k; } else k=next[k]; } // for(int i=0;i<len;i++)cout<<next[i]<<' ';cout<<endl; return next; } int kmp(char *a,char *s) { //int cas=0; int sum=0; int *next=qiu_next(a); int i=0,j=0; while(s[j]!='\0') { if(i<0||a[i]==s[j]) { if(a[i+1]=='\0') { sum++;i=next[i];//cout<<"OK: "<<j<<endl; } else { i++;j++; } } else { i=next[i]; } } return sum; } int main() { char a[10002]; char s[1000002]; int t; scanf("%d",&t);getchar(); while(t--) { gets(a);//cout<<a<<endl; gets(s);//cout<<s<<endl; printf("%d\n",kmp(a,s)); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator