hdu 1711 Number Sequence (KMP)
来源:互联网 发布:阿里云ecs ftp 编辑:程序博客网 时间:2024/06/03 14:14
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 28879 Accepted Submission(s): 12146
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
Source
HDU 2007-Spring Programming Contest
Recommend
lcy
裸的KMP,直接代码
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int t[1000005],p[10005],ne[10005];void makeNext(const int p[],int ne[],int len){ ne[0]=0; for(int i=1,k=0;i<len;i++) { while(k>0&&p[i]!=p[k]) k=ne[k-1]; if(p[i]==p[k]) k++; ne[i]=k; }}int kmp(const int t[],const int p[],int ne[],int n,int m){ int ans=0; makeNext(p,ne,m); for(int i=0,k=0;i<n;i++) { while(k>0&&t[i]!=p[k]) k=ne[k-1]; if(t[i]==p[k]) k++; if(k==m) { ans=i-m+2; return ans; } } return -1;}int main(){ int c; scanf("%d",&c); for(int i=1;i<=c;i++) { int n,m; scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%d",&t[i]); for(int i=0;i<m;i++) scanf("%d",&p[i]); printf("%d\n",kmp(t,p,ne,n,m)); } return 0;}
阅读全文
0 0
- HDU 1711 Number Sequence(kmp)
- HDU 1711 Number Sequence (kmp)
- hdu 1711 Number Sequence--KMP
- hdu kmp 1711 Number Sequence
- HDU 1711 Number Sequence (KMP)
- HDU 1711Number Sequence(KMP)
- Hdu 1711 KMP Number Sequence
- HDU 1711 Number Sequence KMP
- [HDU 1711]Number Sequence[kmp]
- 【KMP】hdu 1711 Number Sequence
- HDU 1711 Number Sequence(KMP)
- hdu 1711 Number Sequence(KMP)
- 【HDU】1711 Number Sequence KMP
- HDU 1711 Number Sequence (KMP)
- hdu 1711 Number Sequence(KMP)
- KMP & hdu 1711 Number Sequence
- hdu 1711 Number Sequence(kmp)
- hdu 1711 Number Sequence(kmp)
- HDU6105 [2017duoxiaolianhe6] Gameia 博弈你个香蕉船
- springmvc 文件下载
- 涨姿势了,蜻蜓FM源码剖析
- 2017HDU多校第6场
- HDU 6098 Inversion
- hdu 1711 Number Sequence (KMP)
- LINUX基本命令
- VMware workstation批量创建虚拟机和自动化安装操作系统(一)
- log4j2 strLookup
- C语言中可变参数的函数
- 查看机器端口占用情况
- poj1258 最小生成树裸题 给个模板看就行了
- 浅谈session和cookie的异同点
- [Python]与shell交互