KMP算法
来源:互联网 发布:mac能打dota2吗 编辑:程序博客网 时间:2024/06/16 07:10
http://acm.hdu.edu.cn/showproblem.php?pid=1711
在杭电上看到了一道KMP算法的题目,于是捎带的学习了一下KMP算法,现贴上AC代码。
#include<iostream>using namespace std;long int a[1000001];long int b[10001];long int next[1000001];void nextfun(){ long int i=1; long int j=0; next[1]=0; while(i < b[0]) { if(0 == j || b[i] == b[j]) { i++; j++; next[i] =j; } else j = next[j]; }}int KMP(){ long int i=1,j=1; while(i<=a[0] && j<=b[0]) { if(a[i] == b[j] || 0 == j) { i++; j++; } else j = next[j]; } if(j > b[0]) return (i - b[0]); else return -1;}int main(){ int t; cin >> t; while(t --) { cin >> a[0] >> b[0]; long int i; for(i=1;i<=a[0];i++) { cin >> a[i]; } for(i=1;i<=b[0];i++) cin >> b[i]; nextfun(); cout<<KMP()<<endl; } return 0;}
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- KMP算法
- Delphi数组
- url 特殊字符 传递参数解决方法
- [转载] 字符集与字符编码简介
- 关于大批量数据上传和更新的方法
- 输入法移植
- KMP算法
- 高斯模糊理论与应用
- videoInput样例
- div+css模仿框架布局
- 市民花百元装Win7“尝鲜”
- ASP.NET(C#)中web.config
- WinForm下的TextBox只能输入整数(可正可负)
- 制作FFmpeg SDK for MS Visual Studio
- oracle Constraint[类似 constraint用法总结 I]