kmp入门---hd1711
来源:互联网 发布:数据库基本概念 编辑:程序博客网 时间:2024/06/10 02:26
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 29786 Accepted Submission(s): 12548
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
213 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 1 313 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 2 1
Sample Output
6-1
一直定义char型的s【】 a【】 一直wr 现在换成int 就过了 真是大意啊
#include<iostream>#include<stdio.h>using namespace std;#include<string>#include<string.h>const int N=1000010;int s[N],a[N];int t,i,n,m,next1[N];int j,k;void init(){ cin>>n>>m; for(i=0;i<n;i++) scanf("%d",&s[i]); for(i=0;i<m;i++) scanf("%d",&a[i]);}void get_next(int *a,int *next1){ k=-1,j=0; next1[0]=-1; while(j<m){ if(k==-1||a[k]==a[j]){ k++; j++; if(a[k]!=a[j]) next1[j]=k; else next1[j]=next1[k]; } else k=next1[k]; }}int kmp(int *s,int *a){ get_next(a,next1); i=j=0; while(i<n){ if(j==-1||a[j]==s[i])i++,j++; else j=next1[j]; if(j==m) return i-j+1; } return -1;}int main(){ scanf("%d",&t); while(t--) { init(); cout<<kmp(s,a)<<endl; } return 0;}
阅读全文
0 0
- kmp入门---hd1711
- kmp模板(HD1711)
- kmp入门--kmp运用
- KMP入门
- KMP入门
- KMP入门
- kmp入门--kmp c++实现
- 扩展kmp入门---扩展kmp
- hdu1711之入门KMP
- hdu2203之KMP入门
- poj1226之kmp入门
- poj3450之kmp入门
- poj3461之kmp入门
- poj3080之KMP入门
- hdu2594之KMP入门
- KMP经典入门理论
- 算法入门之KMP
- hdu1711 kmp入门
- 【Java核心计算 基础知识(第9版)】第4章 对象与类
- python实现给定一个单链表删除指定节点
- 【JZOJ5270】【GDOI2018模拟】神奇的矩阵(二维线段树)
- 【JZOJ5295】【清华集训2017模拟】Create
- 织梦dedecms TAG标签调用汇总(史上最全)
- kmp入门---hd1711
- 时至今日,微信小程序的冰火真相终于爆出
- Teemo Attacking问题及解法
- P1042 乒乓球
- jxl读取excel时间
- 【JZOJ5272】【GDOI2018模拟】神奇的重复序列(DP,性质题)
- $.ajax()方法详解
- 4G LTE再次提速 速度最高可达1.2Gbps
- 织梦dedecms站内seo优化的标签调用代码大全