Number Sequence

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

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
赤裸裸的kmp
 //kmp
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[],b[];
int Next[];
int n,m;
int kmp()
{
int i,j;
j = ;
int tm = Next[] = -;
//ÇóNextÊý×é
while(j<m-){
if(tm<||b[j]==b[tm])
Next[++j] = ++tm;
else tm = Next[tm];
}
//Æ¥Åä
for( i = j = ; i < n&&j < m; ){
if(j<||a[i]==b[j])i++,j++;
else j = Next[j];
}
if(j<m) return -;
return i-j;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i = ; i< n; i++)
scanf("%d",&a[i]);
for(int i = ; i < m; i++)
scanf("%d",&b[i]);
int ans = kmp();
if(ans!=-)
printf("%d\n",ans+);
else puts("-1");
}
return ;
}
 

hdu_1711Number Sequence(kmp)的更多相关文章

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

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

  2. hdu 1711 Number Sequence KMP 基础题

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

  3. hdu 1711Number Sequence (KMP入门,子串第一次出现的位置)

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

  4. HDU 1711 Number Sequence (KMP 入门)

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

  5. Number Sequence(KMP,判断子串 模板)

    题意: 给两数组,求一个是否是另一个的子数组,若是返回匹配的首位置 分析: KMP 入门 //扫描字符串A,并更新可以匹配到B的什么位置. #include <map> #include ...

  6. HDU 1711 Number Sequence KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...

  7. 1711 Number Sequence(kmp)

    Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...

  8. Number Sequence kmp

    Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...

  9. HDU1711 Number Sequence KMP

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

随机推荐

  1. Java JTS & 空间数据模型

    空间数据模型 判断两个几何图形是否存在指定的空间关系.包括: 相等(equals).分离(disjoint).相交(intersect).相接(touches).交叉(crosses).包含于(wit ...

  2. 通过自动回复机器人学Mybatis 笔记:接口式编程

    [接口式编程]尚未遇见Spring --> 代码量反而增加 1.增加约定,减少犯错的可能(不用直接去写字符串 修改点1:命名空间 修改点2:增加接口,方法名与配置文件中的id对应 package ...

  3. iView的使用【小白向】

    首先看这篇:构建Vue本地开发环境(现阶段还不知道怎么用CDN的方式做...) 安装iView(WindowsPowershell或cmd下用cnpm) 编辑上一篇博客创建的Vue工程 先到main. ...

  4. Java中list<Object[]>、list<Student>、list<Map<String,String>>排序

    1:list<Object[]>的排序   public static void main(String[] args) { // TODO Auto-generated method s ...

  5. Linux并发连接上百万的配置

    To support over 500k users, you *need* - A bits hardware/kernel (AMD64, Opterons) - At least 8GB of ...

  6. NumPy学习笔记 三 股票价格

    NumPy学习笔记 三 股票价格 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.&l ...

  7. Hello TensorFlow 三 (Golang)

    在一台ubuntu 16.04.2虚拟机上为golang安装TensorFlow. 官方参考:https://www.tensorflow.org/install/install_go 首先安装go ...

  8. 6、投资的一些思考 - CEO之公司管理经验谈

    对于投资,前面笔者写过一个文:IT人经济思维之投资 - 创业与投资系列文章 ,里面列举了笔者自己做过的投资方面的内容.今天就说说公司投资的一些思考问题. 公司投资的问题,笔者还是那句话:关键是找出适合 ...

  9. Linux服务管理1-1 课程简介与系统运行级别

  10. 【转】npm使用过程中的一些错误解决办法及npm常用命令

    原文 node,npm在前端开发流程中提供了非常完善的自动化工具链,但是同样由于其复杂性导致有很多奇奇怪怪的问题.本文将记录使用过程中出现的一些问题及其解决方法备案. 国内由于gfw问题,导致很多国外 ...