Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17971    Accepted Submission(s): 7854

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
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int a[MAXN],b[MAXN],next[MAXN];
int lena,lenb;
void getnext()
{
int i=,j=-;
next[]=-;
while(i<lenb)
{
if(j==-||b[i]==b[j])
{
i++;
j++;
next[i]=j;
}
else j=next[j];
}
}
int KMP()
{
getnext();
int i=,j=;
while(i<lena&&j<lenb)
{
if(j==-||a[i]==b[j])
{
i++;
j++;
}
else j=next[j];
}
if(j==lenb) return i-j+;
else return -;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(next,,sizeof(next));
scanf("%d%d",&lena,&lenb);
for(int i=;i<lena;i++)
scanf("%d",&a[i]);
for(int i=;i<lenb;i++)
scanf("%d",&b[i]);
printf("%d\n",KMP());
} return ;
}
 

HDU1711(KMP入门题)的更多相关文章

  1. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

  2. hdu 1358 Period(KMP入门题)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. HDU2203(KMP入门题)

    亲和串 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. HDU2087(KMP入门题)

    剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. HUD1686(KMP入门题)

    Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. 题解报告:hdu 2087 剪花布条(KMP入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...

  7. HDU 2222 Keywords Search AC自己主动机入门题

    单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...

  8. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  9. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

随机推荐

  1. Go语言阅读小笔记,来自知呼达达关于unsafe.Pointer的分享.

    第一式 - 获得Slice和String的内存数据 func stringPointer(s string) unsafe.Pointer { p := (*reflect.StringHeader) ...

  2. Odoo电子数据交换(EDI)

    Odoo EDI功能能在odoo实例之间交换数据,可以交换哪些数据呢? 默认支持: account.invoice 发票,含发票行 res.currency res.partner purchase. ...

  3. android客户端向服务器端验证登陆方法的实现1

    遇到的问题:一个条件查询与多个条件查询,所用到的方式不一样 参考文档: http://www.oschina.net/question/1160609_133366    mybatis多条件查询的一 ...

  4. iOS 倒计时NSTimer

    项目中可能会遇到有些倒计时的地方 比方 手机验证的时候,验证码一般都会有一个时间限制,此时在输入验证码的地方就须要展示一个倒计时 详细实现方式是使用了iOS 自带的 NSTimer 上代码 首先新建 ...

  5. Cocos2d-x移植安卓的笔记

    一.下载所需软件 Java SDK   http://www.oracle.com/technetwork/java/javase/downloads/index.html  Windows x64 ...

  6. oracle insert/update

    insert into table (,,) values (,,) where update table set (,)=(select , from ) where

  7. css3背景及字体渐变

    1.背景渐变: .linear { width: 100%; FILTER: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,sta ...

  8. mysql启动warning: World-writable config file

    如果在启动warning: World-writable config file /home/mysql/my.cnf is ignored原因:my.cnf的读取权限进行了设置,不允许World-w ...

  9. 【BZOJ1110】[POI2007]砝码Odw 贪心

    [BZOJ1110][POI2007]砝码Odw Description 在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件恼人的工作.公司有一些固定容量的容器可以装这些砝码 ...

  10. Scrapyd部署

    从github(https://github.com/scrapy/scrapyd)下载安装包放到D:\python\Lib\site-packages\ 解压压缩包:cd 到解压目录 python ...