Given two sequences of numbers : a11, a22, ...... , aNN, and b11, b22, ...... , bMM (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make aKK = b11, aK+1K+1 = b22, ...... , aK+M−1K+M−1 = bMM. If there are more than one K exist, output the smallest one. 

InputThe 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 a11, a22, ...... , aNN. The third line contains M integers which indicate b11, b22, ...... , bMM. All integers are in the range of −1000000,1000000−1000000,1000000. 
OutputFor 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
题意:就是把字符串匹配变成了数组匹配
题解:kmp就行(经典的看毛片算法)参考的这位大牛的博客,讲的很清楚http://blog.csdn.net/starstar1992/article/details/54913261
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; int ext[N],str[maxn],ptr[N];
void getnext(int slen)
{
ext[]=-;
int k=-;
for(int i=;i<=slen-;i++)
{
while(k>-&&str[k+]!=str[i])k=ext[k];
if(str[k+]==str[i])k++;
ext[i]=k;
}
}
int kmp(int plen,int slen)
{
int k=-;
for(int i=;i<=plen-;i++)
{
while(k>-&&str[k+]!=ptr[i])k=ext[k];
if(str[k+]==ptr[i])k++;
if(k==slen-)return i-slen+;
}
return -;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
// cout<<setiosflags(ios::fixed)<<setprecision(2);
int t,n,m;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=;i<n;i++)cin>>ptr[i];
for(int i=;i<m;i++)cin>>str[i];
getnext(m);
cout<<kmp(n,m)<<endl;
}
return ;
}

hdu1711kmp的更多相关文章

  1. hdu--1711--kmp应用在整形数组--Number Sequence

    /* Name: hdu--1711--Number Sequence Author: shen_渊 Date: 16/04/17 19:58 Description: 第一次知道,KMP能用在整形数 ...

随机推荐

  1. 对Vue.js $watch方法的理解

    博主最近对着vue.js的官方教程在自学vue.js,博主自幼愚钝,在教程中真的是好多点都不太理解,接下来要说的这个$watch方法就是其中一个不太理解的点了.咱们先来看一下对于$watch方法在vu ...

  2. JAVA基础知识(2)--关键字final的使用

    /***Final关键字的使用*@author lihaiming*Email:912547587@qq.com*关键字Final可以修饰属性,方法,类*修饰属性的时候,final修饰的变量是一个常量 ...

  3. oracle学习 笔记(2)

    题记:在使用Oracle数据库的时候,发现Oracle是没有自动增长列来实现主键的,所以在此记录学习.(PS:如果哪里有错误或者不足的地方还请大家帮忙指出来) 二.序列(自动增长列) 为此问题博主也是 ...

  4. 自适应滤波:维纳滤波器——GSC算法及语音增强

    作者:桂. 时间:2017-03-26  06:06:44 链接:http://www.cnblogs.com/xingshansi/p/6621185.html 声明:欢迎被转载,不过记得注明出处哦 ...

  5. swig编译GDAL的C#库时遇到的代码安全问题及解决方法

    之前一直用的是别人编译好的gdal库开发,今天自己编译了gdal的2.0.0版本,踩了不少坑,但总算解决了. 编译方法主要参考http://blog.csdn.net/liminlu0314/arti ...

  6. javascript的闭包与一次重构的感受

    有没有这么一个场景,你的一个动作需要在所有异步方法执行完毕后,再进行操作?然而你对异步方法何时执行完毕感到困扰,只能在每个方法中写回调,在回调中重复劳动? 偶然的,想起了之前经理讲过的闭包的概念,偶然 ...

  7. JSP九大内置对象的作用和用法总结(转)

    SP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page.exception 1.r ...

  8. nginx 入门配置

    这个星期公司的定期分享内容是Nginx,于是就要写作业了. 一.动静分离 1.下载Windows 版本的Nginx,解压,放到C盘下.进入目录,然后按然shift键右键,打开命令行,输入: start ...

  9. Tcl与Design Compiler (十一)——其他的时序约束选项(二)

    本文如果有错,欢迎留言更正:此外,转载请标明出处 http://www.cnblogs.com/IClearner/  ,作者:IC_learner 前面介绍的设计都不算很复杂,都是使用时钟的默认行为 ...

  10. 解决eclipse中文字很小

    新下载的eclipse4.2.1版本,显示中文字体很小,但是英文比较正常.网上查看要更改字体大小,但是更改后英文也变大了,不是想要的结果. window – preferences – general ...