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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath> using namespace std; int an[];
int bn[];
int nextt[];
int match[]; int main()
{
int z,n,m,i,j,k;
cin>>z;
while(z--)
{
scanf("%d%d",&n,&m);
for(i = ;i<=n;i++)
scanf("%d",&an[i]);
for(i = ;i<=m;i++)
scanf("%d",&bn[i]);
nextt[] = ;
for(i = ;i<=m;i++)
{
int t = nextt[i-];
while(t&&bn[i]!=bn[t+]) t = nextt[t];
if(bn[i] == bn[t+]) t++;
nextt[i] = t;
}
match[] = ;
bool b = ;
for(i = ;i<=n;i++)
{
int t = match[i-];
while(t&&an[i] != bn[t+]) t = nextt[t];
if(an[i] == bn[t+]) t++;
match[i] = t;
if(t == m)
{
cout<<i-m+<<endl;
b = ;
break;
}
}
if(!b) cout<<-<<endl;
}
return ;
}

hdu1711Number Sequence的更多相关文章

  1. HDU-1711-Number Sequence(KMP)(Rabin-Karp)

    Rabin-Karp Accepted 1711 904MS 5272K 1310 B G++ #include "bits/stdc++.h" using namespace s ...

  2. hdu--1711--kmp应用在整形数组--Number Sequence

    /* Name: hdu--1711--Number Sequence Author: shen_渊 Date: 16/04/17 19:58 Description: 第一次知道,KMP能用在整形数 ...

  3. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  4. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  5. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  6. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  8. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  9. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

随机推荐

  1. C# LiNq的语法以及常用的扩展方法

    首先先来扯一下,这篇博文是我第一次写的,主要是我的一些摘录,希望对大家有所帮助. Linq的基础 •LINQ(读音link):Linq To SQL(过时).Linq To Object.Linq T ...

  2. Java 面向对象(转)

    转自:http://blog.sina.com.cn/s/blog_83c5190f01010ate.html 1.我们可以把JAVA中的类分为以下三种: 类:使用class定义且不含有抽象方法的类. ...

  3. Visual Studio宏注释模板

    前言 有时写代码需要写注释的时候 甚是苦恼 写吧 怕麻烦 不写吧 似乎这代码估计自己都看不懂 权衡之下 似乎找一个自动写注释的方法最靠谱 一直在VS下开发 偶尔听人说过有一个宏工具可以帮助开发者快速注 ...

  4. c#接口定义与应用

    public interface IBankAccount //只能加public修饰符,或者什么都不加 { void Playin(decimal money); //函数前不加任何修饰符号 boo ...

  5. 恶补ASP.NET基础【1】枚举和结构

    有时我们希望变量提取的是一个固定集合中的值,此时就可以用枚举类型, 例: enum OpenMode : byte { 新增=, 编辑=, 查看= } class Program { static v ...

  6. gl.TexSubImage2D 使用遇到图片翻转的问题

    这2天在用gl.TexSubImage2D把几张小图转拼接成大图,如果在渲染物体之前拼接好就没有问题,但在开始渲染物体后拼接就会有问题.后来我做了2件事情来找原因, 1. 用拼好的图来画一个正方形,大 ...

  7. Intent、Bundle——实现Activity之间的通信

    http://blog.sina.com.cn/s/blog_62dea93001015847.html 一个应用程序会有多个Activity,但是只有一个Activity作为程序的入口,应用中的其他 ...

  8. javascript 事件设计模式

    http://plkong.iteye.com/blog/213543 1. 事件设计概述 事件机制可以是程序逻辑更加清晰可见,在JavaScript中很多对象都有自己的事件,如:button有onc ...

  9. BCB的博客,以及例子(好多传输文件的例子)

    http://blog.csdn.net/keyu1711/ http://download.csdn.net/user/keyu1711 http://download.csdn.net/user/ ...

  10. JIRA项目跟踪管理工具简介与安装

    1.什么是JIRA JIRA是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领域. Atlassian2002年 ...