poj 3461
来源:互联网 发布:windows phone账号忘了 编辑:程序博客网 时间:2024/05/17 06:04
next[]数组,next[j]的值表示当模式中第j个字符与主串中相应字符失配时,在模式串中需要重新和主串中该字符进行比较的字符的位置。
对于next[]数组的定义如下:
1)next[j]=0 j=1
2)next[j]=max k:0<k<j P[1...k-1]=P[j-k+1,j-1]
3)next[j]=1 其他
#include<stdio.h>#include<string.h>char s1[1001000],s2[10010];int next[10010];void getnext(){int i=1,j=0,k;next[1]=0;k=strlen(&s2[1]);while(i<=k){if(j==0 || s2[i]==s2[j]){i++,j++;next[i]=j;}elsej=next[j];}}int kmp(){int i=1,j=1,sum=0,k1,k2;k1=strlen(&s1[1]);k2=strlen(&s2[1]);while(i<=k1){if(j==0 || s1[i]==s2[j])++i,++j;elsej=next[j];if(j>k2){sum++;j=next[j];}}return sum;}int main(){int t,T;scanf("%d",&T);for(t=1;t<=T;t++){scanf("%s%s",&s2[1],&s1[1]);getnext();printf("%d\n",kmp());}}
- poj 3461
- POJ 3461
- poj 3461
- poj 3461
- POJ 3461
- poj 3461
- poj 3461
- POJ 3461
- poj 3461
- poj 3461
- POJ 3461
- POJ 3461
- POJ 3461
- POJ 3461
- poj 3461
- POJ 3461
- POJ 3461
- poj 3461
- Eclipse下将web项目部署到网站根目录
- 读书记录
- 题目1 求2个已排序的表的交与并
- 嘿牛程序员__成都传智博客__JavaScript中的复选框的全选、反选问题
- 黑客活动合法化?荷兰在野党提议DDoS攻击合法
- poj 3461
- 中科院系统内计算机相关方向研究实力点评
- 观后感--《冠军》
- 小偷问题
- 黑马程序员_.NET学习1(vs2010中用.net4.0的bug)
- Internals of Java Class Loading
- 黑马程序员之ado.net之一
- linux环境变量配置的4个方法
- MMC 卡驱动分析