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为什么不过?In Reply To:kmp为什么不过? Posted by:sailors at 2007-11-08 17:46:56 #include<iostream> #include<cstring> using namespace std; #define N 1000100 struct String { char s[N]; int len; }; String S, T; int next[N]; int getnext() { int i,j; next[1]=0; j=0; for(i=2;i<=T.len;i++) { while(j>0&&T.s[j+1]!=T.s[j])j=next[j]; if(T.s[j+1]==T.s[i])j=j+1; next[i]=j; } return 0; } int kmp() { int i,j; j=0; int cn=0; for(i=1;i<=S.len;i++) { while(j>0&&T.s[j+1]!=S.s[i])j=next[j]; if(T.s[j+1]==S.s[i])j=j+1; if(j==T.len) { //printf("%dggg \n",i-T.len); cn++; j=next[j]; } } return cn; } char ss[N],tt[N]; int main() { int i; int ca; int len1; //freopen("kmp.in","r",stdin); //freopen("kmp.out","w",stdout); scanf("%d",&ca); { while(ca--) { scanf("%s%s",tt,ss); len1=strlen(ss); S.len=len1; for(i=0;i<len1;i++) { S.s[i+1]=ss[i]; } len1=strlen(tt); T.len=len1; for(i=0;i<len1;i++) { T.s[i+1]=tt[i]; } getnext(); int res=kmp(); printf("%d\n",res); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator