kmp“看毛片”算法模板题
来源:互联网 发布:红包透视软件 编辑:程序博客网 时间:2024/04/24 15:22
kmp“看毛片”算法模板题
HDU1711
Problem Description
Given two sequences of numbers : a[1], a[2], …… , a[N], and b[1], b[2], …… , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], …… , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.Input
The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], …… , a[N]. The third line contains M integers which indicate b[1], b[2], …… , b[M]. All integers are in the range of [-1000000, 1000000].Output
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
Sample Input
2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1Sample Output
6
-1Source
HDU 2007-Spring Programming ContestRecommend
lcy | We have carefully selected several similar problems for you: 1358 3336 1686 3746 1251
这个算法很不好理解,个人表示看了很久才略懂,然后找了这个题来做。大意是:给两个数列,在A数列中找到B数列,并输出第一次找到B数列的位置(从B数列第一个数开始的第一个位置),若找不到则输出-1。
这道题就是直接套kmp模板,用next【】数组来保存位移
如B数组为1 2 3 1 3 1 2 3 1 1 3
那么next为 1,1,1,2,1,2,3,4,5,2,1
代码
#include<cstdio>#include<iostream>#include<cstring>using namespace std;int wenben[1000005],moban[10005];int next1[10005];int n,m;void makenext(){ int k=1,q; next1[1]=1; for(q=2;q<=m;q++) { while(k>1&&moban[k]!=moban[q]) k=next1[k-1]; if(moban[k]==moban[q]) k++; next1[q]=k; } return ;}void kmp(){ int k=1,q,ans=-1; for(q=1;q<=n;q++) { while(k>1&&moban[k]!=wenben[q]) k=next1[k-1]; if(moban[k]==wenben[q]) k++; if(k==m+1) { ans=q-m+1; break; } } printf("%d\n",ans); return ;}int main(){ int t; cin>>t; while(t--) { cin>>n>>m; for(int i=1;i<=n;i++) cin>>wenben[i]; for(int i=1;i<=m;i++) cin>>moban[i]; makenext(); kmp(); } return 0;}
- kmp“看毛片”算法模板题
- KMP-看毛片算法
- kmp(看毛片)算法详解
- KMP-看毛片算法 c++
- 看毛片(KMP)算法简析
- 初次接触kmp看毛片算法
- 字符串匹配KMP(看毛片)算法
- 字符串匹配之看毛片算法-KMP
- KMP(看毛片)-
- 菜鸟都能理解的看毛片(KMP)算法
- KMP (看毛片算法) 养成篇 (一)(启发篇)
- HDU--杭电--1711--Number Sequence--KMP--水题,看毛片算法基础死方法运用
- poj 3461 (看毛片算法初探)
- 【字符串匹配】——KMP(看毛片算法)——深入讲解next数组的求解
- 【KMP】KMP算法模板
- 萌新的看毛片(kmp)学习日记(下标从0开始)
- hdu 1711 KMP算法模板题
- HDU2087(KMP算法模板题)
- SQL 数据维护语言的设计及应用实验
- 二分插入排序
- java继承
- SPI协议
- 参考文献对齐的设置方法,希望对你有用
- kmp“看毛片”算法模板题
- java中接口和抽象类的区别
- 第一天学习笔记
- MFC、SDK和API有什么区别
- 自学-ES6篇-Symbol
- JS关于数组的基本知识点
- Spring MVC 流程图
- 关于HTML5中解析left或者top等的值时出错,声明被丢弃问题解决
- 算法学习笔记之卡尔曼滤波算法理解