Number Sequence

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

Total Submission(s): 40053    Accepted Submission(s): 16510

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

#include<iostream>
#include<string.h>
using namespace std;
int tt,pp;
int t[1000011],p[11111],next1[11111];
void getnext()
{
int i=0,j=-1;
next1[0]=-1;
while(i<pp-1)
{
if(j==-1||p[i]==p[j])
next1[++i]=++j;
else
j=next1[j];
}
return;
}
int kmp()
{
int i=0,j=0;
while(i<tt)
{
if(j==-1||t[i]==p[j])
i++,j++;
else
j=next1[j];
if(j==pp)
return i-j+1;
}
return -1;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&tt,&pp);
for(int i=0;i<tt;i++)
scanf("%d",&t[i]);
for(int i=0;i<pp;i++)
scanf("%d",&p[i]);
getnext();
printf("%d\n",kmp());
}
return 0;
}

HDU1711 Number Sequence(KMP模板题)的更多相关文章

  1. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  2. hdu1711 Number Sequence kmp模板

    题目传送门 学习博客 学习了kmp算法,理解了算法思想,但还没有到能把这个思想用语言来描述出来. #include<bits/stdc++.h> #define clr(a,b) mems ...

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

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

  4. HDU1711 Number Sequence KMP

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU1711 题意概括 给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出 ...

  5. hdu 1711 Number Sequence KMP 基础题

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

  6. hdu1711 Number Sequence kmp应用

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目: Problem Description Given two sequences of n ...

  7. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  8. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

  9. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

随机推荐

  1. JVM总结(二):JVM的内存分配策略

    这节我们总结一下JVM中的内存分配策略.目录如下: 内存分配策略 对象优先在新生代Eden分配 大对象直接进入老年代 长期存活的对象将进入老年代 动态对象年龄判定 空间分配担保 内存分配策略 Java ...

  2. 部署flask

    部署 当我们执行下面的hello.py时,使用的flask自带的服务器,完成了web服务的启动.在生产环境中,flask自带的服务器,无法满足性能要求,我们这里采用Gunicorn做wsgi容器,来部 ...

  3. 一个Entity Framework、ADO.NET查询性能测试

    Entity Framework自然是会比ADO.NET性能慢点,这个不多说了.直接上结果. 本该用测试项目的,不过我建了个aspx.下面是随便测20遍得到的结果 补充!!把12行改成 list = ...

  4. windows 查看链接数

    windows 端口链接数  查看有效 链接数: netstat -an|find " |find "ESTABLISHED" /c /c 统计: 查看链接信息: 查看当 ...

  5. [C++]指针/指针数组/数组指针/多维指针/单值指针/多值指针

    int main(){ //单值指针(指向单个值得指针,类同普通变量) int *px = new int; *px = 100; printf("%d",*px); delete ...

  6. logback配置按天产生日志文件

    1 依赖Jar包 pom配置 也可以根据自己的版本来 <dependency> <groupId>org.slf4j</groupId> <artifactI ...

  7. nvm安装与使用

    1.nvm是什么 nvm全名node.js version management,顾名思义是一个nodejs的版本管理工具.通过它可以安装和切换不同版本的nodejs.下面列出下载.安装及使用方法. ...

  8. HOG feature

  9. L-BFGS算法(转载)

    转载链接:http://blog.csdn.net/itplus/article/details/21897715 前面的拟牛顿法.DFP.BFGS.L-BFGS算法简短总结一下就是: 牛顿法不仅使用 ...

  10. Linux MMC framework2:基本组件之host

    声明:本文很多内容和思路参考了http://www.wowotech.net/comm/mmc_host_driver.html,对原作者表示感谢! 1.前言 本文是Linux MMC framewo ...