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. [wikioi2069]油画(贪心)

    题目:http://www.wikioi.com/problem/2069/ 分析: 首先这个问题比较复杂,涉及到两个重要的考虑点,一个是当前拿来的颜色是否保留,一个是若保留后那么应该把当前盘子的哪个 ...

  2. [USACO2006][poj3182]The Grove(巧妙的BFS)

    题目:http://poj.org/problem?id=3182 题意:一个棋盘中间有一个联通块,给你一个起点让你从起点开始绕联通块外围一圈并回到起点,求最小步数. 分析: 首先根据数据的范围比较小 ...

  3. ACL权限的学习

    ACL ACL:访问控制列表,其主要作用是将一些"用户"加到表中,并对这些用户的行为进行控制. 案例: 有个文件夹project是root用户创建,并且关于这个文件夹有以下权限 d ...

  4. please tell me the error about java Graphics

    this is the code: public class paint extends JFrame{ public paint(){ setLayout(new FlowLayout()); se ...

  5. js简单的工厂模式

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  6. Java Web-session介绍

    使用情况 Session对象记载某一特定的客户信息,不同的客户用不同的Session对象来记载 Session对象有效期:默认为20分钟,可设定 Session工作原理:在应用程序中,当客户端启动一个 ...

  7. Android Studio能干什么

    建立系统工具包可以用来生成,测试,运行您的应用程序和软件包.构建系统是独立于Android的工作室,所以你可以调用它的Android的工作室或从命令行.在你写你的应用程序,你可以使用编译系统的特点: ...

  8. 【Gym 100685J】Just Another Disney Problem(交互/排序)

    第一次做交互题. 题意是有n个数(n<1000),你通过问1 a b,后台返回你YES代表a<b,NO代表a>b.要你在10000次询问内给出一个符合的排列.n=1000来说,100 ...

  9. 【ZOJ 1221】Risk

    题 题意 给你20个城市的相邻关系,求给定任意两个城市的最短距离 分析 求任意两个城市最短距离,就是用floyd算法,我脑残忘记了k是写在最外层的. 代码 #include<stdio.h> ...

  10. Android中实现自定义的拍照应用

    可以参考:http://www.android-doc.com/guide/topics/media/camera.html 一.添加相应的权限 <uses-permission android ...