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. vForum 2014点滴随笔

    vForum2014 的口号:NO Limits 纵横无限 一条好消息:VMware 将在中国建立亚洲研究院,并在5年内投资10亿美元. VMware宋先生的演讲再次印证了Redhat会议上的趋势: ...

  2. 利用Python自动发送邮件

    # -*- coding:utf-8 -*-from email.mime.text import MIMETextfrom email.header import Headerimport smtp ...

  3. d3js 添加数据

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  4. MySQL提示Access denied for user &#39;&#39;@&#39;localhost&#39;”的解决

    记得那时由于没有网络,把rootpassword改错了写成了: update user set password="122" where user="root" ...

  5. C语言判断回文数

    #include<stdio.h> #include<stdlib.h> int main() { //1.得到这个数字 2.翻转 3.进行比较 4.如果相同 就输出 是 否则 ...

  6. js event 的target 和currentTarget

    target  点击的实际tag currentTarget 绑定事件的target

  7. 自动添加QQ

    自动添加QQ <meta http-equiv="refresh" content="0; url=tencent://AddContact/?fromId=50& ...

  8. 在“云基础设施即服务的魔力象限”报告中,AWS 连续三年被评为领导者

    在"2014 云基础设施即服务的魔力象限"中.Gartner 将 Amazon Web Services 定位在"领导者象限"中,并评价 AWS 拥有最完整.最 ...

  9. Git配置的用户名密码在本地的存贮位置

    全局的用户名密码配置: //配置用户名和邮箱(全局) $ git config --global user.name "j***n" $ git config --global u ...

  10. 前后端通吃的单元测试---mocha

    git clone https://github.com/shenggen1987/mocha-demo.git npm install front 前台测试代码 backend 后台测试代码 先安装 ...