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. 9. iptables 配置

    iptables 配置文件存放位置:  [root@Demon yum.repos.d]# vim /etc/rc.d/init.d/iptables   一.只给 Centos 6.5 打开 22 ...

  2. filezilla无法连接linux服务器

    问题描述: 响应: 220 (vsFTPd 2.2.2)命令: AUTH TLS错误: 无法连接到服务器状态: 已从服务器断开 排查步骤: 1 检查服务器IP地址.用户名.密码是否正确 2 在控制面板 ...

  3. Android SQLite的使用1(非原创)

    1.继承SQLiteOpenHelper :public class MyOpenHelper extends SQLiteOpenHelper {} 2.重写下面3个方法 package com.e ...

  4. Emmet 语法探析

    Emmet 语法探析 Emmet(Zen Coding)是一个能大幅度提高前端开发效率的一个工具. 大多数编辑器都支持Snippet,即存储和重用一些代码块.但是前提是:你必须先定义 这些代码块. E ...

  5. HDU 1071 - The area

    求曲线和直线围成的面积 求表达式,求积分 #include <iostream> using namespace std; ],y[]; int t; double k,m;//fx1: ...

  6. Java三大特征之继承(二)

    在<Think in java>中有这样一句话:复用代码是Java众多引人注目的功能之一.但要想成为极具革命性的语言,仅仅能够复制代码并对加以改变是不够的,它还必须能够做更多的事情.在这句 ...

  7. JavaScript总结之单击弹出div

    今天也算用了不少手段来实现他们的要求,大概记录一下,下边的代码示例,我全部修改贴出来,争取全部占到自己的代码里就能用. 1.点击同一个div,打开/关闭另一个div. 1 <script typ ...

  8. CSS发抖

    纯CSS发抖  当您在 @keyframes 中创建动画时,请把它捆绑到某个选择器,否则不会产生动画效果. 通过规定至少以下两项 CSS3 动画属性,即可将动画绑定到选择器: 规定动画的名称 规定动画 ...

  9. CKfinder中文乱码的解决.

    最近在写一个类似博客的系统,使用了ckeditor和ckfinder,但是发现ckfinder在上传中文文件名的文件过程中会出现中文乱码的情况. 于是百度google乎,发现大多数的解决办法都是将文件 ...

  10. ASCII码排序,hdu-2000

    Problem Description 输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符.   Input 输入数据有多组,每组占一行,有三个字符组成,之间无空格.   Output ...