Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30893    Accepted Submission(s): 12981

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 1

Sample Output

6
-1

Source

HDU 2007-Spring Programming Contest

Recommend

lcy

题解:

      ①KMP入门吧。

      ②贴一个板子,以前总是不熟练(打自己一下,啊!)。

#include<stdio.h>
#define go(i,a,b) for(int i=a;i<=b;i++)
int n,m,f[10003],j,C,T[1000004],P[10003];
int main()
{
scanf("%d",&C);
while(C--&&scanf("%d%d",&n,&m))
{
go(i,0,n-1)scanf("%d",T+i);
go(i,0,m-1)scanf("%d",P+i);f[0]=f[1]=0;
go(i,1,m-1){j=f[i];while(j&&P[i]!=P[j])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}j=0;
go(i,0,n-1){while(j&&P[j]!=T[i])j=f[j];j+=P[j]==T[i];
if(j==m){printf("%d\n",i-j+2);goto _;}}puts("-1");_:;
}
return 0;
}//Paul_Guderian

Birds don't just fly they fall down and get up,
Nobody learns without getting it won.——————<Zootopia>

【HDU 1711 Number Sequence】的更多相关文章

  1. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  2. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  3. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

  4. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  5. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 1711 Number Sequence (KMP简单题)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu 1711 Number Sequence KMP 基础题

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. KMP - HDU 1711 Number Sequence

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. HDU 1711 Number Sequence(字符串匹配)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. python_29_三级菜单

    menu={ '北京':{ '海淀':{ '五道口':{ '搜狐':{}, '网易':{}, 'Google':{}, }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, '优酷':{}, ...

  2. 漫谈 Clustering (番外篇): Expectation Maximization

    Expectation Maximization (EM) 是一种以迭代的方式来解决一类特殊最大似然 (Maximum Likelihood) 问题的方法,这类问题通常是无法直接求得最优解,但是如果引 ...

  3. samba性能调优,调优后,性能增加30%

    global中增加下面内容. [global]    use sendfile = yes    write raw = yes    read raw = yes    max xmit = 655 ...

  4. kindeditor 上传图片失败问题总结

    1.近段时间一直在处理kindeditor上传图片失败的问题,前期一直以为是前端的问题,利用谷歌控制台,打断点,修改方法,一直都找不到解决方案,直到查看服务器配置,才发现: WEB 1号服务器 /da ...

  5. window10启用administrator 和启用组策略编辑器

    1,启用administrator账户 net user administrator /active:yes 2,启用组策略编辑器    新建一个文本文件.把下面代码粘贴进去.修改后缀名为.cmd  ...

  6. iOS 反射函数: performSelector, NSInvocation, objc_msgSend

    当我们有方法名和参数列表,想要动态地给对象发送消息,可用通过反射函数机制来实现,有两种常用的做法: 一.performSelector - (id)performSelector:(SEL)aSele ...

  7. cf550C. Divisibility by Eight(结论)

    题意 给出长度为$n$的字符串,判断是否能删除一些数后被$8$整除 Sol 神仙题啊Orz 结论: 若数字的后三位能被$8$整除,则该数字能被$8$整除 证明 设$x = 10000 * a_i + ...

  8. python常用内置算法用到的单词音频

    http://boscdn.bpc.baidu.com/v1/developer/990a728b-ca96-4bd9-9124-5357d829bf70.mp3 百度广播开发平台生成

  9. python3 练习题100例 (八)

    题目八:暂停一秒输出,并格式化当前时间. #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 题目八:暂停一秒输出,并格 ...

  10. 算法_NP_证明

    8.3 STINGY SAT is the following problem: given a set of clauses (each a disjunction of literals) and ...