poj3461-我的第一道kmp
来源:互联网 发布:sql declare 全局变量 编辑:程序博客网 时间:2024/04/26 07:47
好像一直没有看懂的样子,直到昨天晚上顿悟了!其实kmp与一般的最朴素匹配的想法一样只是他每一次失配后都不是从头开始,而是根据一定的规则来使子串t从失配的位置重新配;而失配的位置就是靠next数组来记录的。而next数组的求法主要是:在子串T内next[0]=-1;如果t[i]!=t[j],j=next[j],之后再继续比较;如果相等next[i]=j;而kmp的求法和next相近,只是比较的对象发生了变化由t[i]与t[j]比较变成s[i]与t[j]的比较;下面上代码:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int maxn=1000005;int ls,lt;int Next[maxn];char s[maxn],t[maxn];inline void getNext(){ int i,j=-1; Next[0]=-1; for(int i=1;i<lt;i++){ while(j!=-1&&t[i]!=t[j+1])j=Next[j]; if(t[i]==t[j+1])j++; Next[i]=j; }}inline int kmp(){ getNext(); int i,j=-1,sum=0; for(int i=0;i<ls;i++){ while(j!=-1&&s[i]!=t[j+1])j=Next[j]; if(s[i]==t[j+1])j++; if(j==lt-1){ sum++; j=Next[j]; } } return sum;}int main(){ int cas; scanf("%d",&cas); while(cas--){ scanf("%s%s",t,s); ls=strlen(s); lt=strlen(t); printf("%d\n",kmp()); } return 0;}
0 0
- poj3461-我的第一道kmp
- Oulipo poj3461 简单的KMP
- poj3461(KMP)
- POJ3461 KMP
- KMP+poj3461
- poj3461 KMP
- poj3461 KMP
- poj3461 KMP
- POJ3461 KMP
- poj3461 KMP
- POJ3461-串匹配-经典的KMP
- 出的第一道题--陈公公找女友--【KMP】
- 我的第一道数位dp,hdu3555
- 我的poj第一道数据结构--poj1035
- UVa101-我的第一道uva
- poj3461(kmp模版题)
- 【KMP】 poj3461 Oulipo
- poj3461 kmp 模板题
- hex_dump
- Java中CardLayout卡片布局管理器使用的小例子
- 游戏服务器之被动连接线程池
- Android Path路径设置,针对error opening trace file:No such file or directory
- c语言--交换两个数的函数
- poj3461-我的第一道kmp
- 关于指针、引用、值传递的学习
- 谈谈singelton单例模式
- 中国陆地生态系统变化的空间特征
- 日志
- Caused by: java.lang.NoSuchMethodException: javax.xml.bind.annotation.XmlElementRef.required()
- Java 数组length总结
- 1111111111
- FIFO