标签:ring std ons c++ pre char ret amp har
#include<iostream> #include<cstring> #include<cstdio> using namespace std; const int nMax = 10005; const int mMax = 1000005; char text[mMax],pat[nMax]; int lent,lenp,next[nMax]; void get_next() { int i,j = -1; next[0]=-1; for(i=0;i<lenp;i++) { while(j>-1&&pat[j+1]!=pat[i]) j=next[j]; next[i]=j; } } int KMP() { int ans=0,i=0,j=-1; get_next(); for(i=0;i<lent;i++) { while(j!=-1&&pat[j+1]!=text[i]) { j=next[j]; } if(pat[j+1]==text[i]) j=j+1; if(j==lenp-1) ans++; } return ans; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%s%s",pat,text); lenp=strlen(pat); lent=strlen(text); printf("%d\n",KMP()); } return 0; }
标签:ring std ons c++ pre char ret amp har
原文地址:http://www.cnblogs.com/enyala/p/7694254.html