HDU 1711:Number Sequence(KMP模板,求位置)
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 40388 Accepted Submission(s): 16659
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
题意
给出两个数组,问第二个数组是不是第一个数组的子序列,如果是,输出第二个数组的第一个元素在第一个数组中的位置,否则输出-1
AC代码
#include <bits/stdc++.h>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn=1e6+10;
int a[maxn],b[maxn];
int Next[maxn];
int n,m;
// Next数组下标从1开始
void getNext()
{
int j,k;
j=0;k=-1;Next[0]=-1;
while(j<m)
{
if(k==-1||b[j]==b[k])
Next[++j]=++k;
else
k=Next[k];
}
}
/*
返回第二个数组在第一个数组中首次出现的位置
返回的位置是从0开始的。
*/
int KMP_Index()
{
int i=0,j=0;
getNext();
while(i<n&&j<m)
{
if(j==-1||a[i]==b[j])
{
i++;j++;
}
else
j=Next[j];
}
if(j==m)
return i-m;
else
return -1;
}
int main(int argc, char const *argv[])
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
ms(a);
ms(b);
ms(Next);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);
if(KMP_Index()!=-1)
printf("%d\n",KMP_Index()+1);
else
printf("%d\n",KMP_Index());
}
return 0;
}
HDU 1711:Number Sequence(KMP模板,求位置)的更多相关文章
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1711 Number Sequence KMP 基础题
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1711 Number Sequence (KMP 入门)
Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...
- HDU 1711 Number Sequence KMP
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...
- HDU 1711 Number Sequence(KMP)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- HDU 1711 Number Sequence(KMP模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...
随机推荐
- 把旧系统迁移到.Net Core 2.0 日记(4) - 使用EF+Mysql
因为Mac 不能装SqlServer, 所以把数据库迁移到MySql,然后EntityFramework要改成Pomelo.EntityFrameworkCore.MySql 数据库迁移时,nvarc ...
- ie edge 自动给数字加下划线
<meta name="format-detection" content="telephone=no,email=no,address=no">
- static与全局变量区别
作用域不同: 全局变量是不显式用static修饰的全局变量,但全局变量默认是动态的,作用域是整个工程,在一个文件内定义的全局变量,在另一个文件中,通过extern 全局变量名的声明,就可以使用全局变量 ...
- linux系统管理 系统文件
常用的目录作用 '/' 根目录 '/bin' 命令保存目录(普通用户读取的命令) '/boot' 启动目录,启动相关文件 '/dev' 设备文件保存目录 '/etc' 配置文件保存目录 '/home' ...
- leetcode 刷题 数组类 Two Sum
---恢复内容开始--- Two Sum Given an array of integers ,find two numbers such that they add up to a specifi ...
- nginx:在linux上进行nginx的安装
-----1.安装 换源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 下载: wget ...
- jQuery中的事件与驱动
1.jQuery中的事件 在jQuery中,事件总体分为俩大类:基础事件和符合事件. jQuery中的简单事件,与Javascript中的事件 几乎一样,都含有鼠标事件.键盘事件.载件事件等,只是其 ...
- Cracking The Coding Interview5.2
//Given a (decimal - e.g. 3.72) number that is passed in as a string, print the binary representatio ...
- [转]如何远程连接运行OpenGL/Cuda 等GPU程序
发现一篇神文,解决了困扰许久的远程桌面OpenGL/GPU 等问题... 原地址在这:http://www.tanglei.name/how-to-run-gpu-programs-using-rem ...
- MPX
Pre-processing: MPX使用的滤波器是自适应性的‘Wiener’ filter.有时甚至不用MPX,因为尽管Wiener 所引起的扰乱是可以忽略的,有时发现滤波并不能很好地提高拾取的精度 ...