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. 2台linux机器免密码互相登陆

    一,如何使2台机器不需要密码互相登陆到对方呢? 这个和使用密钥的登陆Linux系统非常相似,也是将自己的公钥传到要登录的服务器上去修改权限即可. 1,A机器: 执行ssh-keygen命令,一路回车. ...

  2. C# 枚举运用"位"操作和"或"操作

    定义: /// <summary> /// The js function type(the same as name). /// </summary> [Flags] pub ...

  3. oracle中job定时调用存储过程的实例

    使用job模拟定时从元数据表中抽取指定数据进入目标表的过程. 一.创建元数据表 --create table test_origianl create table test_original_data ...

  4. Javascript-数据类型、类型转换

    typeof 判断数据类型: var n = 1; var t = "echo"; var fn = function() {} var arr = [1,2,3]; typeof ...

  5. 组合数(DFS)

    组合数 点我   描述 找出从自然数1.2.... .n(0<n<10)中任取r(0<r<=n)个数的所有组合.   输入 输入n.r. 输出 按特定顺序输出所有组合.特定顺序 ...

  6. Android: ScrollView监听滑动到顶端和底端

    在项目中需要监听ScrollView滑动到顶端和底端的时候以实现自己的ScrollView,那么怎样去监听呢?今天查看了一下ScrollView的源码,找到了一种方法.先看一下源码中的overScro ...

  7. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  8. SQL 临时表或表变量替代游标

    1.如果表没有自动增长的标识列(int) 使用临时表 SELECT IDENTITY(int) NewID ,.. INTO #tmp FROM YouTable 2.表有标识列 使用表变量 INSE ...

  9. stream~filestream

    http://blog.csdn.net/feliciafay/article/details/6157356 http://blog.csdn.net/feliciafay/article/deta ...

  10. inno setup 跳过(Welcome)欢迎界面

    原文 http://zwkufo.blog.163.com/blog/static/25882512010816049549/ 在InnoSetup中,我们很容易用 function ShouldSk ...