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. Android研究之手PullToRefresh(ListView GridView 下拉刷新)使用具体解释

     群里一哥们今天聊天偶然提到这个git hub上的控件:pull-to-refresh ,有兴趣的看下,样例中的功能极其强大,支持非常多控件.本篇博客具体给大家介绍下ListView和GridVi ...

  2. UVA - 12130 Summits

    Description Problem G - Summits Time limit: 8 seconds You recently started working for the largest m ...

  3. 得知Android小遴选程序第七头(他们定义对话框、Gallery、ImageSwitcher)

    效果如下面的:            一共一个activity和两个xml. ******当我们须要使用的组件不在setContentView()设置的布局文件里,那我们就须要使用inflate()方 ...

  4. Twitter实时搜索系统EarlyBird

    twitter要存档tweet采用lucene做全量指数,新发型是实时索引推文.检索实时(10在几秒钟内指数).实时索引和检索系统,称为EarlyBird. 感觉写更清晰,简洁,这个信息是真实的,只有 ...

  5. C#如何获得 WINDOWS 版本号

    using System; using System.Runtime.InteropServices; namespace GetWindowsVersion { [ StructLayout( La ...

  6. java 加载dll介绍(转)

    最近在做的工作要用到本地方法,需要在Java中加载不少动态链接库(以下为方便延用Windows平台下的简写dll,但并不局限于Windows).刚刚把程序跑通,赶紧把一些心得写出来,mark.也希望对 ...

  7. SDUTOJ 1298 活动选择

    #include<iostream> #include<memory.h> using namespace std; int a[105],b[105],c[105],d[10 ...

  8. mysql出现Data truncated for column

    1.首先叙述,描述你自己的问题. 假设你想把同意为空值的勾去掉.. .恰巧数据库中的数据 processed_f 为空没有值. . . .这时候你对数据表结构做更改的时候就会出现Data trunca ...

  9. 【Android基础】点击Back键退出应用程序

    //第一种方法(弹出对话框) @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-genera ...

  10. HDU 1596 find the safest road (最短路)

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...