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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1358 3336 3746 1867 2203 

题解:kmp模板 编译错误了多次,无爱啦...

代码:

 #include <iostream>
#include <cstdio>
using namespace std;
int p[],s[];
int n,m;
int nex[]; void get()
{
int plen=m;
nex[]=-;
int k=-,j=;
while(j < plen){
if(k==- || p[j] == p[k]){
++j;
++k;
if(p[j] != p[k])
nex[j]=k;
else
nex[j]=nex[k];
}
else{
k=nex[k];
}
}
} int kmp()
{
int i=,j=;
int slen=n;
int plen=m;
while(i < slen && j< plen){
if(j==- || s[i]==p[j]){
++i;
++j;
}
else{
j=nex[j];
}
}
if(j == plen)
return i-j+;
else
return -;
} int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
scanf("%d",&s[i]);
for(int i=; i<m; i++)
scanf("%d",&p[i]);
get();
printf("%d\n",kmp());
}
}

1711 Number Sequence(kmp)的更多相关文章

  1. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. hdu 1711 Number Sequence KMP 基础题

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 1711 Number Sequence (KMP 入门)

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

  4. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  5. HDU 1711 Number Sequence KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...

  6. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  7. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

  8. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  9. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

随机推荐

  1. linux上安装配置vsftpd(转)

    转自:http://www.webarch.org/category/linux生产环境是Centos5.6,由于需要提供ftp服务,就找了一款比较小巧的ftp服务器端软件,vsftpd(vsftpd ...

  2. [LeetCode129]Sum Root to Leaf Numbers

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

  3. 华为编程大赛汇总----java

           以下华为编程比赛题目都是网上整理得到的,代码都是自己调试过的,由于网上java答案较少,欢迎大家批评指正,也希望对准备华为上机的童鞋们有一点点帮助.在练习的过程中成长,加油!~~  1. ...

  4. mac 下有些工具 app 推荐

    打开推荐报价mac在工具app Evernote的 Evernote的贬值,课堂笔记软件,其主要功能是记录笔记,假设你想,它可用于使todo list, 甚至gtd, 或收集的知识归纳 Doit.im ...

  5. 关于csrss.exe和winlogon.exe进程多、占用CPU高的解决办法

    原地址 http://blog.sina.com.cn/s/blog_912e77480101nuif.html   最近VPS的CPU一直处在100%左右,后台管理上去经常打不开,后来发现上远程都要 ...

  6. Windows8.1和Ubuntu14.04双系统卸载Ubuntu參考教程[图]

    我之前编写并公布了<Windows8.1安装Ubuntu14.04双系统參考教程及双硬盘注意事项>这篇教程,而本文提供的卸载Ubuntu方法适用于这篇教程,其它方法安装的Ubuntu可适当 ...

  7. Apple Watch 1.0 开发介绍 1.3 简介 WatchKit App 架构

    WatchKit app和WatchKit extension一起实现了app的界面.当用户在Apple Watch中操作时,WatchKit app从storyboards中选择合适的场景.比如,如 ...

  8. 了解ASP.NET5 Web应用程序结构

    本文参考ASP.NET5 官方文档 Understanding ASP.NET 5 Web Apps,加入了一些个人理解,理解不对的地方希望大家能指出,互相学习. ASP.NET 5 针对WEB编程引 ...

  9. 最小二乘法拟合非线性函数及其Matlab/Excel 实现(转)

    1.最小二乘原理 Matlab直接实现最小二乘法的示例: close x = 1:1:100; a = -1.5; b = -10; y = a*log(x)+b; yrand = y + 0.5*r ...

  10. 最新jhost免费jsp云空间会员邀请码

    jhost支持jsp.php的免费云空间,邀请码用于激活空间服务: 邀请码:20141003104317_149661                  有效期:2014-10-03 http://w ...