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.
InputThe 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].

OutputFor 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 首先是预处理Next数组,初始化Next[0]=-1,j=-1 然后从i=0开始,如果j!=-1&&不匹配,j=Next[j]。否则判断p[++i]==p[++j],如果相等,直接让Next[i]=Next[j],不等的话,让Next[i]=j; 然后在主串里匹配。i=0,j=0; j!=-1&&不匹配, j=Next[j]。否则i++,j++
 #include<stdio.h>
#include<string.h>
int Next[],t[],p[];//模式串对应的Next
int n,m,_; void prekmp() {//预处理Next数组
int i,j;
j=Next[]=-;
i=;
while(i<m) {
while(j!=-&&p[i]!=p[j]) j=Next[j];
if(p[++i]==p[++j]) Next[i]=Next[j];//会快一点 博客体现了这一点
else Next[i]=j;
}
} int kmp() {//查找在主串的位置
int i=,j=;
prekmp();
while(i<n&&j<m) {
while(j!=-&&t[i]!=p[j]) j=Next[j];
i++;j++;
}
if(j==m) return i-m+;
else return -;
} int main() {
for(scanf("%d",&_);_;_--) {
scanf("%d%d",&n,&m);
for(int i=;i<n;i++) {
scanf("%d",&t[i]);
}
for(int i=;i<m;i++) {
scanf("%d",&p[i]);
}
int ans=kmp();
printf("%d\n",ans);
}
}

kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence的更多相关文章

  1. kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  2. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  4. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  5. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条

    一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...

  8. kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)

    After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...

随机推荐

  1. Solaris10上如何识别新增加的HDLM LUN

    先在磁盘阵列上将新加LUN映射给主机组,然后在光纤交换机上增加相关zone信息.以下是Solaris10上需要执行的操作步骤. 在Solaris10上重新扫描磁盘 -bash-3.2# cfgadm ...

  2. 10-30SQLserver基础--(备份和还原、分离和附加数据库)、语句查询操作

    一.数据库是一个大容量的存储数据的仓库,为了保证数据完整性,防止一些数据的意外丢失等情况,需要对数据进行备份和还原. 备份数据不影响数据库的正常运行. 1.备份.还原数据库 首先对数据库进行备份,操作 ...

  3. 类型:Oracle;问题:oracle 查询表详细信息;结果:oracle查询表信息(索引,外键,列等)

    oracle查询表信息(索引,外键,列等) oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息,索引信息查询SQL如下,希望对大家有所帮助: 1.查询出所有的用户表sel ...

  4. jQuery实现页内锚点平滑跳转

    当页面内容长多,导致页面高度过高或过宽是,浏览起来就有点费劲,不过使用了锚点平滑跳转效果可以实现页面的跳转,从而加快速浏览想要浏览的模块.具体做法如下: 首先是菜单(锚点)的写法 <a href ...

  5. python 2.7.5升级到3.4.x

    wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz .tgz cd Python-/ Python ./configure ma ...

  6. Javascript面向对象(二):构造函数的继承

    这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个" ...

  7. dubbo-admin打包和zookper安装

    1 首选安装Zookper,下载zookeeper-3.5.3-beta版本,在这里我主要演示这个:下载地址:http://mirrors.hust.edu.cn/apache/zookeeper/ ...

  8. css知多少(5)——选择器(转)

    css知多少(5)——选择器   1. 引言 从本节开始,就进入本系列的第二个部分——css和html的结合——说白了就是选择器. CSS中定义了样式,如何将这些样式设置到相应的html节点上?就不得 ...

  9. 指定jdk编译或运行

    set JAVA_HOME=D:\java\jdk8 set CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOMe%\lib\tools.jar; set Pat ...

  10. 框架之 hibernate之各种查询

    1. Hibernate的查询方式 2. Hibernate的查询策略 案例:使用Hibernate完成查询所有联系人功能 需求分析 1. 完成所有的联系人的查询 技术分析之Hibernate框架的查 ...