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. Sublime Text插件FileHeader实践

    FileHeader是一个文件模板插件,可以定制各种文件模板和文件头部信息,保存时可以自动更新文件的修改时间.在多人开发中这个功能相当实用. 具体介绍我就不细说了,主要是分享一下在使用FileHead ...

  2. Android studio教程:[6]创建多个Activity

    通常来说,一个android应用程序不止一个Activity(活动),更不止一个界面.于是需要创建多个Activity来满足应用程序的要求,这里我将告诉大家如何添加新的Activity,并实现Acti ...

  3. iOS 使用自定义的字体

    一.新建一个工程,准备好要使用的字体,后缀为.ttf或者.otf格式. 二.将字体直接拖入工程项目中. 三.在Info.plist中添加一个新的Key:Fonts provided by applic ...

  4. JS时间戳与日期类型格式相互转换

    function datetime_to_unix(datetime){     var tmp_datetime = datetime.replace(/:/g,'-');     tmp_date ...

  5. MapXtreme在asp.net中的使用之加载地图(转)

    MapXtreme在asp.net中的使用之加载地图(转) Posted on 2010-05-04 19:44 Happy Coding 阅读(669) 评论(0) 编辑 收藏 1.地图保存在本地的 ...

  6. C++在数组元素个数未知情况下声明数组

    我们都从书上学习的方法,定义一个数组需要数组名.类型以及数组元素个数,一般定义必须明确元素的个数,否则无法通过编译. 1. int a[]; 2. int n; int a[n]; 就想上面这两种情况 ...

  7. Marineking wilyin

    A - Marineking wilyin Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Ot ...

  8. CentOS6.4 LAMP环境搭建

    网上的教程,不能按着抄打进去,这样会打乱你环境放置位置, 会导致配置路径会出问题. 要有一个环境目录优化, 把环境文件都装在/usr/local里面 首先,把安装文件rar都放置在/usr/local ...

  9. 【ecos学习4】[转]ubuntu 11.04 tftp 设置

    安装 TFTP 服务 sudo apt-get install xinetdsudo apt-get install tftp-hpasudo apt-get install tftpd-hpa 修改 ...

  10. Leetcode 100 Same Tree python

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...