kmp的应用,求一个模式串在文本串中出现了几次
如果发现一个匹配后,令j=next[j]即可,即再接下去尝试匹配
代码:
j=next[j];
}
//cout<<p+1<<endl;
if(p[j+1]==t[i])
{
//cout<<t[j+1]<<" "<<t[i]<<endl;
j++;
//cout<<"j="<<j<<endl;
}
//cout<<i<<" "<<j<<endl;
if(j==tar)
{
cnt++;
j=next[j];
}
}
cout<<cnt<<endl;
}
int main()
{
int i,j,n;
scanf("%d",&n);
while(n--)
{
scanf("%s%s",p+1,t+1);
kmp();
}
return 0;
}