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. (22)Properties,这不会Io流中的类,但是通常和IO流中的一些流配合使用

    可以和流相关联的集合对象Properties. Map |--Hashtable |--Properties Properties:该集合不需要泛型,因为该集合中的键值对都是String类型.既然是m ...

  2. linux下大于2T的硬盘格式化方法

    我们先在超级用户模式下用fdisk -l命令查看挂载的硬盘设备,假设设备号为/dev/sdb,接下来我们使用parted命令来进行GPT分区:1. yum install parted -y# par ...

  3. linux 安装sysstat使用iostat、mpstat、sar、sa

    使用yum安装 #yum install sysstat sysstat的安装包是:sysstat-5.0.5-1.i386.rpm,装完了sysstat-5.0.5-1.i386.rpm后 就会有i ...

  4. pyqt4实现tab界面切换

    (转:https://segmentfault.com/q/1010000005143428)

  5. 个人作业2——英语学习APP案例分析

    一.个人体验 1.下载并使用,描述最简单直观的个人第一次上手体验. ①入眼界面华丽,有正能量的名言警句配上很有意境的图片,界面美观. ②内容丰富,有许多精选英文文章,同时配有中文翻译,便于理解. ③能 ...

  6. Oracle存储过程的调用和执行

    1.什么是存储过程: 用于在数据库中完成特定的操作或者任务.是一个PLSQL程序块,可以永久的保存在数据库中以供其他程序调用. 2.无参存储过程的使用: Normal 0 7.8 磅 0 2 fals ...

  7. 手机自动化测试:Appium源码分析之跟踪代码分析七

    手机自动化测试:Appium源码分析之跟踪代码分析七   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest推出手机自 ...

  8. EDP转换IC NCS8803:HDMI转EDP芯片

    HDMI-to-eDP Converter w/ scaler1 Features    Embedded-DisplayPort (eDP) Output    1/2/4-lane eDP @ 1 ...

  9. Lombok : 让你写 Java代码像C#一样爽

    前言 我曾经是一名 .Net 开发,如今的我是一名 Java 开发者.在我享受着 Java 成熟的生态时,我常常怀念 c# 简洁的语法:自动属性.类型推断.自动初始化器 .... 鱼,我所欲也,熊掌亦 ...

  10. Mac系统-java环境搭建_01

    一.安装jdk 下载地址:http://www.oracle.com/technetwork/Java/javase/downloads/index-jsp-138363.html 1.傻瓜式安装下一 ...