题目地址: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. highCharts图表应用-模拟心电图

    通过前两章的学习,相信大家对highcharts已经有了初步的了解.这一章将通过一个例子来模拟Highcharts如何实现经常变化的数据显示. 比如说股票的涨停.实时篮球比分以及A选手和B选手的支持率 ...

  2. 【python】pyqt练习

    import sys from PyQt4.QtCore import * from PyQt4.QtGui import * import ui_price class PriceDlg(QDial ...

  3. 第10步:DBCA创建实例

    注意,创建磁盘组时需要以oracle用户身份执行,在那之前可能需要以root身份执行xhost+,即命令: 代码1 [root@sgdb1~]# xhost+ [root@sgdb1~]# su - ...

  4. Android中的ACCESS_MOCK_LOCATION权限使用Demo

    转载地址:http://mobiarch.wordpress.com/2012/07/17/testing-with-mock-location-data-in-android/ The DDMS t ...

  5. 把xml格式的字符串写入到一个xml文件中

    package demo; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; impo ...

  6. Asynchronous calls and remote callbacks using Lingo Spring Remoting

    http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...

  7. 使用apt-get命令,出现“无法获得锁”的问题

    参考地址 http://blog.csdn.net/namecyf/article/details/7297168

  8. python中的 try...except...finally 的用法

    python中的 try...except...finally 的用法 author:headsen chen date:2018-04-09  16:22:11 try, except, final ...

  9. cannot be cast to javax.servletFilter

    java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast ...

  10. 巨蟒python全栈开发-第18天 核能来袭-类和类之间的关系

    一.今日主要内容: 1.类与类之间的关系 在我们的世界中事物和事物之间总会有一些联系. 在面向对象中,类和类之间也可以产生相关的关系 (1)依赖关系 执行某个动作(方法)的时候,需要xxx来帮助你完成 ...