#include<iostream>
#include<cstring>
using namespace std;
char s1[1000005];
char s2[1005];
int p[10005];
int l1,l2;
void init()
{
int i,j;
i=0;
l2 = strlen(s2);
j=-1;
p[0]=-1;
while(i<l2)
{
if(j==-1||s2[i]==s2[j])
{
p[++i]=++j;
}
else
j=p[j];
}
}
int cot;
void kmp()
{
int i,j;
int l1,l2;
l1=strlen(s1);
l2=strlen(s2);
i=-1;
j=-1;
while(i<l1)
{
if(j==-1||s1[i]==s2[j])
{
i++;
j++;
}
else
{
j=p[j];
}
if(j==l2)
{
cot++;
j=0; //避免重叠
}
}
}
int main()
{
while(cin>>s1)
{
cot=0;
if(s1[0]=='#')
break;
cin>>s2;
init();
kmp();
cout<<cot<<endl;
}
}
hdu 2087 减花布条 kmp
最新推荐文章于 2021-10-18 20:27:02 发布