题目地址: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. [Tips]Javascrip计算文件行数

    function calcLineCount(filename, callback) { var fs = require('fs'); var fileStream = fs.createReadS ...

  2. Unity3D和网页数据交互的基本原理

    简介: 1.Unity3D的游戏引擎是和编辑器集成在一起的,所有它也是一个制作/开发平台. 2.Unity3D是使用JavaScript.C#作为核心脚本语言来驱动事个游戏引擎. 3.平台可以发布Ex ...

  3. 如何交换a,b的数值——一个简单的问题就证明现在的你依然弱爆了

    How? int c = a; a = b; b = c; 这样会浪费多一点内存去存放c,so还有吗? a ^= b; b ^= a; a ^= b; ^:异或. 答案碉堡了, 不过估计除了有可能在面 ...

  4. MySQL Error: Illegal mix of collations for operation 'concat'

    在使用concat连接字符串时出现错误:MySQL Error: Illegal mix of collations for operation 'concat' 原因:字段操作默认为UTF8的编码, ...

  5. 浅析Java与C#的事件处理机制

    http://www.cnblogs.com/OOAbooke/archive/2012/02/18/2356899.html

  6. java的Date类型转换为MySQL数据库的Date类型

    最近遇到一个问题,需要把java中的日期类型存放为MySQL数据库的日期类型,两个日期之间需要进行转化才能进行存储,转化代码如下: package com.alphajuns.demo1; impor ...

  7. 72、android状态栏一体化,状态栏改变颜色

         只能在4.4以上版本使用. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  8. Windows下将gvim8配置为Python IDE

    目录 1.准备工作 2.安装 3.配置 _vimrc 4.编写和编译运行程序 正文 Windows下将gvim配置为Python IDE 回到顶部 1.准备工作 将下面的安装包或者文件下载好 1) P ...

  9. python的猴子补丁monkey patch

    monkey patch指的是在运行时动态替换,一般是在startup的时候. 用过gevent就会知道,会在最开头的地方gevent.monkey.patch_all();把标准库中的thread/ ...

  10. springMVC的url-pattern /和/*的区别

    总之,关于web.xml的url映射的小知识: <url-pattern>/</url-pattern> 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样 ...