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. IDA Pro权威指南学习笔记(一)

    一直不懂逆向,最近刚好不忙,于是学习逆向,用来做笔记,顺便和大家分享交流. 参考书籍<IAD PRO权威指南> 工具: PETools: ETools 是另一款很好的PE文件编辑工具,以前 ...

  2. Solaris10如何确认DirectIO是否已经启用

    对于Oracle而言,如果数据库存储在UFS文件系统上,启用DirectIO能够提高数据库性能.Oracle有个参数filesystemio_options可以控制数据库是否使用DirectIO.  ...

  3. 蓝牙服务 UUID

    https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx 手机蓝牙对手机 ,华为平板取红米手机 8 个Audio So ...

  4. C#WinForm如何调整控件的Tab按键顺序

    在日常生活中,很多用户都会有使用Tab键的习惯.而在C#的WinForm开发中,Tab按键的顺序默认是你拖拽进窗体的顺序.那么我们如何修改这个顺序呢?答案如下(以VS2010为例). 只需要点击[视图 ...

  5. eclipse中创建maven web项目

    本文主要说明将maven web项目转成eclipse支持的web项目. 创建一个maven项目设置打包类型为war则其为web项目 结构如下 将mavenweb项目转成eclipse识别的web项目 ...

  6. 超详细的Maven使用教程

    原文:  http://blog.csdn.net/u010425776/article/details/52027706 主题 Maven 什么是Maven? 如今我们构建一个项目需要用到很多第三方 ...

  7. 数据库监听。数据库一次notify,Activity多次接收

    今天项目中发现一个bug: (1)当uri数据库中有更新,会从数据库层DataService中通知应用层,调用notifyChange: mContext.getContentResolver().n ...

  8. SQL基础E-R图画法

    例一.假设有以下表:T1(a1,a2, a3, a5)T2(a3,a4)T3(a5, a6)T4(a3, a5, a7)其中带下划线的属性标识为所在关系模式的主码T1中的a3是参照T2的外码T1中的a ...

  9. win10 更换秘钥报错:拒绝访问:所请求的操作需要提升特权

    直接打开cmd执行换秘钥的命令: slmgr /ipk VK7JG-NPHTM-C97JM-9MPGT-3V66T slmgr /skms kms.xspace.in slmgr /ato 报错如图: ...

  10. ROS Learning-010 beginner_Tutorials 编写简单的启动脚本文件(.launch 文件)

    ROS Indigo beginner_Tutorials-09 编写简单的启动脚本文件 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.0 ...