POJ 3461 Oulipo
这道题说到底,就是给你N组子串和母串,然后求每组中子串在母串中出现的次数(包括重叠)。一道KMP裸题,献上KMP模板:
1 #include<stdio.h> 2 #include<string.h> 3 #define maxn 2333333 4 int np,ns,n,next[maxn]; 5 char p[maxn],s[maxn]; 6 int read(); 7 void MakeNext(); 8 int KMP(); 9 int main(){ 10 n=read(); 11 for(int i=1;i<=n;i++){ 12 memset(p,0,sizeof(p)); 13 memset(s,0,sizeof(s)); 14 scanf(" %s %s",p+1,s+1); 15 np=strlen(p+1);ns=strlen(s+1); 16 MakeNext(); 17 printf("%d\n",KMP()); 18 } 19 return 0; 20 } 21 /*int read(){ 22 int ans=0,f=1;char c=getchar(); 23 while('0'>c||c>'9'){if(c=='-')f=-1;c=getchar();} 24 while('0'<=c&&c<='9')ans=ans*10+c-48,c=getchar();return ans*f; 25 }*/ 26 void MakeNext(){ 27 memset(next,0,sizeof(next)); 28 int j=0; 29 for(int i=2;i<=np;i++){ 30 while(j&&p[i]!=p[j+1]) j=next[j]; 31 if(p[i]==p[j+1])j++; 32 next[i]=j; 33 } 34 } 35 int KMP(){ 36 int con=0,j=0; 37 for(int i=1;i<=ns;i++){ 38 while(j&&s[i]!=p[j+1])j=next[j]; 39 if(p[j+1]==s[i]) j++; 40 if(j==np){ 41 con++; 42 j=next[j]; 43 } 44 } 45 return con; 46 }