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. [poj1742]coin

    题意:多重背包的w=v特殊情况 分析:此题如果用单调队列O(nv)会被无耻卡常数…… 然后鉴于此题W=V,所以只存在背包恰好为i的是否存在,即bool型的f[i]是否为1 即往背包染色上面考虑 如果是 ...

  2. [wikioi2926][AHOI2002]黑白瓷砖(Polya定理)

    小可可在课余的时候受美术老师的委派从事一项漆绘瓷砖的任务.首先把n(n+1)/2块正六边形瓷砖拼成三角形的形状,右图给出了n=3时拼成的“瓷砖三角形”.然后把每一块瓷砖漆成纯白色或者纯黑色,而且每块瓷 ...

  3. android之对话、单复框的使用

    对话框 通过builder来构建一个单选框 package xidian.dy.com.chujia; import android.content.DialogInterface; import a ...

  4. HTML5——将图片拖拽上传

    如下图所示: 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  5. 解决HTML5布局,兼容IE问题

    当我们使用h5的新标签,header,footer,aside,section,article...时,会遇到低版本IE不兼容问题,如下图: 解决方案:引入如下JS代码,即可 (这里我就直接放源码了, ...

  6. navigationBar设置透明度

    将NavigationBar设置透明(仅将指定视图控制器进行透明处理),步骤如下:1.在视图控制器的头文件中实现UINavigationControllerDelegate,例如:@interface ...

  7. android学习——Android Studio下创建menu布局文件

    一.问题: android studio项目中没有看到menu文件夹: 在android studio项目中想要添加menu布局文件,一开始我的做法是:直接在res文件夹右键选择xml文件来添加,如下 ...

  8. Asp.Net MVC 中实现跨域访问

    在ASP.Net webapi中可以使用  Microsoft.AspNet.WebApi.Cors  来实现: public static class WebApiConfig { public s ...

  9. Java编程思想学习(十四) 枚举

    关键字enum可以将一组具名的值有限集合创建一种为新的类型,而这些具名的值可以作为常规的程序组件使用. 基本enum特性 调用enum的values()方法可以遍历enum实例,values()方法返 ...

  10. .net String.Format数字格式化输出

    内容转载自:http://www.cnblogs.com/lqb/archive/2008/08/04/1259498.html 前面内容这个做的总结的很全,今后有新增的我继续往后补充.请留意我增加的 ...