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 |
234#include<iostream> #include<cstdio> #include<cstring> using namespace std; char S[1000006],T[1000006]; int mjs[1000006]; int lens,lent; int kmp(int mjs[]){ int i,j; int ans=0; j=0; for(i=0;i<lens;i++){ while(j&&S[i]!=T[j]) j=mjs[j]; if(S[i]==T[j]) j++; if(j>=lent) ans++,j=mjs[j]; } return ans; } void work(int lenx,int wxz[]) { wxz[1]=0; for(int i=2;i<=lenx;i++) { int j=wxz[i-1]; while(j&&T[j]!=T[i-1]) j=wxz[j]; if(T[j]==T[i-1]) j++; wxz[i]=j; } } int main() { int t; scanf("%d",&t); for(int ss=1;ss<=t;ss++){ scanf("%s%s",T,S); lens=strlen(S); lent=strlen(T); work(lent,mjs); int love_mjs=kmp(mjs); printf("%d\n",love_mjs); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator