Number Sequence

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

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
 
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int s[],t[];
int Next[];
int ans;
void getNext(int b){
int i=,j=-;
Next[]=-;
while(i<b){
if(j==-||t[i]==t[j]){
i++;
j++;
Next[i]=j;
}
else
j=Next[j];
}
}
int kmp(int a,int b){
int i=,j=,sum=;
while(i<a){
if(j==-||s[i]==t[j]){
i++;
j++;
}
else
j=Next[j];
if(j==b){
ans=i+-b;///该步需要特别注意一下即可
return ans;
}
}
return -;
}
int main(){
int tt;
scanf("%d",&tt);
while(tt--){
memset(s,,sizeof(s));
memset(t,,sizeof(t));
memset(Next,,sizeof(Next));
int t1,t2;
scanf("%d%d",&t1,&t2);
int temp;
for(int i=;i<t1;i++){
scanf("%d",&s[i]);
}
for(int i=;i<t2;i++){
scanf("%d",&t[i]);
}
getNext(t2);
int pd;
pd=kmp(t1,t2);
printf("%d\n",pd); }
return ;
}

HDU1711的更多相关文章

  1. hdu1711(终于搞懂了KMP算法了。。)

    题意:给你两个长度分别为n(1 <= N <= 1000000)和m(1 <= M <= 10000)的序列a[]和b[],求b[]序列在a[]序列中出现的首位置.如果没有请输 ...

  2. HDU1711 Number Sequence KMP

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU1711 题意概括 给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出 ...

  3. hdu1711 KMP

    #include<stdio.h> #include<string.h> #define maxn 1000010 int next[maxn],s[maxn],p[maxn] ...

  4. KMP的模版实现(以hdu1711为例)

    贴代码,觉得带template的有一些大材小用……不过还是按自己风格写吧! /************************************************************* ...

  5. [裸KMP][HDU1711][Number Sequence]

    题意 找到子串在母串出现的第一个位置 解法 裸的KMP 特别的地方 第一次不看模板自己敲的KMP #include<stdio.h> const int maxn=100000; cons ...

  6. hdu1711 Number Sequence

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

  7. HDU1711:Number Sequence

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

  8. 【HDU1711】Number Sequence

    题面 大致题意: 给定两个数列A,B,长度分别为N和M 求出 满足 Ak=B1 ,Ak+1=B2......Ak+M-1=Bm 的最小k值 如果有多个k值输出最小的一个 题解 KMP裸题 直接计算B数 ...

  9. hdu-1711(hash)

    题意:给你T组数据,每组数据分别输入n,m和长度为n的数字数组,和长度为m的数字数组,问你长度为m的数组第一次出现在长度为n的数组的位置 解题思路:标准字符串匹配问题,一般用kmp解,拿来练hash ...

随机推荐

  1. java网络编程基础

    前言 通过网络进行数据传输时,一般使用TCP/UDP进行数据传输.但是两个的区别就是TCP可靠,UDP不可靠.两个的共同之处就是都需要建立socket套接字,将IP地址和端口port进行绑定.但是服务 ...

  2. Nodejs学习笔记(五)--- Express安装入门与模版引擎ejs

    目录 前言 Express简介和安装 运行第一个基于express框架的Web 模版引擎 ejs express项目结构 express项目分析 app.set(name,value) app.use ...

  3. 另一套Oracle SQL练习题,更新参考答案

    题干: create table student( sno ) primary key, sname ), sage ), ssex ) ); create table teacher( tno ) ...

  4. offsetLeft, offsetTop以及postion().left , postion().top有神马区别

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Mysql-字段类型

    首先统计所有,以表格查看 数字类型 列类型 需要的存储量 TINYINT 1 字节 SMALLINT 2 个字节 MEDIUMINT 3 个字节 INT 4 个字节 INTEGER 4 个字节 BIG ...

  6. Maven-在eclipse中安装Maven插件

    装IDE Plugins的方法有很多. 其一:在线安装 通过Help-->Install New Software的方式,输入HTTP地址来安装,简单易操作,但是也优缺点,就是下载速度慢,或者有 ...

  7. Java设计模式-建造者模式(Builder)

    将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示. [构建与表示分离,同构建不同表示] 与抽象工厂的区别:在建造者模式里,有个指导者,由指导者来管理建造者,用户是与指导者联系的,指 ...

  8. 《疯狂Java:突破程序员基本功的16课》读书笔记-第二章 对象与内存控制

    Java内存管理分为两个方面:内存分配和内存回收.这里的内存分配特指创建Java对象时JVM为该对象在堆内存中所分配的内存空间.内存回收指的是当该Java对象失去引用,变成垃圾时,JVM的垃圾回收机制 ...

  9. c#截图

    private void Form_Load(object sender, EventArgs e){  //接收web url  string colle = string.Empty;  stri ...

  10. 3.Android之单选按钮RadioGroup和复选框Checkbox学习

    单选按钮和复选框在实际中经常看到,今天就简单梳理下. 首先,我们在工具中拖进单选按钮RadioGroup和复选框Checkbox,如图: xml对应的源码: <?xml version=&quo ...