POJ 3461 Oulipo KMP模板题
找来测代码
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<climits> #include<string> #include<map> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; typedef long long ll; typedef pair<int,int> pii; #define pb(a) push_back(a) #define INF 0x1f1f1f1f #define lson idx<<1,l,mid #define rson idx<<1|1,mid+1,r #define PI 3.1415926535898 template<class T> T min(const T& a,const T& b,const T& c) { return min(min(a,b),min(a,c)); } template<class T> T max(const T& a,const T& b,const T& c) { return max(max(a,b),max(a,c)); } void debug() { #ifdef ONLINE_JUDGE #else freopen("d:\\in.txt","r",stdin); // freopen("d:\\out1.txt","w",stdout); #endif } int getch() { int ch; while((ch=getchar())!=EOF) { if(ch!=' '&&ch!='\n')return ch; } return EOF; } //kmp const int MAX_STR_LEN=11000; void GetNext(const char *s,int *next) { next[0]=next[1]=0; for(int i=1;s[i]!='\0';i++) { int j=next[i]; if(j&&s[j]!=s[i])j=next[j]; next[i+1]=s[j]==s[i]?j+1:0; } } int kmp(const char *s,const char *sub) { int next[MAX_STR_LEN]; GetNext(sub,next); int num=0; int j=0; for(int i=0;s[i]!='\0';) { if(s[i]==sub[j]) { i++,j++; if(sub[j]=='\0') { num++; j=next[j]; } }else if(j==0) i++; else j=next[j]; } return num; } char str[1001000]; char word[MAX_STR_LEN]; int main() { int t; scanf("%d",&t); for(int ca=1;ca<=t;ca++) { scanf("%s%s",word,str); int num=kmp(str,word); printf("%d\n",num); } return 0; }