题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711

题目:

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
 

思路:裸的kmp应用,直接用kmp就好了,就当练手

代码:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define PI acos((double)-1)
#define E exp(double(1))
#define K 1000000+9
int n,m;
int nt[K],a[K],b[K];
void kmp_next(void)
{
memset(nt,,sizeof(nt));
for(int i=,j=;i<m;i++)
{
while(j&&b[i]!=b[j])j=nt[j-];
if(b[i]==b[j])j++;
nt[i]=j;
}
}
int kmp(void)
{
if(m>n)return -;
kmp_next();
for(int i=,j=;i<n;i++)
{
while(j&&a[i]!=b[j])j=nt[j-];
if(a[i]==b[j])j++;
if(j==m)
return i-m+;
}
return -;
}
int main(void)
{
int t;cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=;i<n;i++)
scanf("%d",&a[i]);
for(int i=;i<m;i++)
scanf("%d",&b[i]);
printf("%d\n",kmp());
} return ;
}

hdu1711 Number Sequence kmp应用的更多相关文章

  1. HDU1711 Number Sequence KMP

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

  2. hdu1711 Number Sequence kmp模板

    题目传送门 学习博客 学习了kmp算法,理解了算法思想,但还没有到能把这个思想用语言来描述出来. #include<bits/stdc++.h> #define clr(a,b) mems ...

  3. HDU1711 Number Sequence(KMP模板题)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. hdu 1711 Number Sequence KMP 基础题

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 1711 Number Sequence (KMP 入门)

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

  7. kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence

    Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...

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

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

  9. HDU1711 Number Sequence 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:最基础的字符串匹配,只不过这里用整数数组代替了字符串. 给你两个数组 \(a[1..N ...

随机推荐

  1. spring mvc 3.0 实现文件上传功能

    http://club.jledu.gov.cn/?uid-5282-action-viewspace-itemid-188672 —————————————————————————————————— ...

  2. [Buzz Today]2013.08.18

    # Go 语言实现memcached:groupcache memcached作者Brad Fitzpatrick用Go语言重新实现了memcached. groupcache继承了memcached ...

  3. 封装常用的selenium方法

    package com.yk.userlive.base; import java.net.MalformedURLException;import java.net.URL;import java. ...

  4. 哈希表类Hashtable

    哈希表是一种重要的存储方式,也是一种常见的检索方法.其基本思想是将关系码的值作为自变量,通过一定的函数关系计算出对应的函数值,把这个数值解释为结点的存储地址,将结点存入计算得到存储地址所对应的存储单元 ...

  5. AWT提供了Java Applet 和Java Application中可用的用户图形界面 GUI 中的基本组件

    AWT提供了Java Applet 和Java Application中可用的用户图形界面 GUI 中的基本组件( component s). 由于Java是一种独立于平台的 程序设计语言 ,但GUI ...

  6. 上下居中css

    .css{ position: relative, top: 50%, transform: translateY(-50%) }

  7. java三大框架SSH(Struts2 Spring Hibernate)

    http://www.cnblogs.com/qiuzhongyang/p/3874149.html

  8. linux 环境 tomcat 莫名奇妙挂掉

    ::-exec-] org.apache.coyote.http11.Http11Processor.service Error processing request java.lang.NullPo ...

  9. Android无线测试之—UiAutomator UiObject API介绍六

    手势操作 1.手势相关操作 2.相关API介绍 返回值 API 描述 boolean performMultiPointerGesture(PointerCoords[]... touches) 执行 ...

  10. C/C++求职宝典重点笔记

    这是我之前准备找工作时看<C/C++求职宝典>一书做的笔记,都是一些笔试面试中常考的重点难点问题,但比较基础,适合初学者看. 1. char c = '\72'; 中的\72代表一个字符, ...