题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char s[1005],p[1005];
int next[1005];
void getnext(char *p,int *next){
next[0]=-1;
int i=0,j=-1,len=strlen(p);
while(i<len){
if(j==-1 || p[i]==p[j]){
i++;
j++;
next[i]=j;
}
else{
j=next[j];
}
}
}
int kmp(char *s,char *p){
int len=strlen(s),len2=strlen(p);
getnext(p,next);
int i=0,j=0,ans=0;
while(i<len){
if(j==-1 || s[i]==p[j]){
i++;
j++;
}
else
j=next[j];
if(j==len2){
ans++;
j=0;
}
}
return ans;
}
int main(){
while(1){
scanf("%s",s);
if(s[0]=='#')
break;
scanf("%s",p);
printf("%d\n",kmp(s,p));
}
}