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 ...
随机推荐
- Java逻辑
1.开发简单Java应用程序 1-1:什么是程序 程序:为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集合.1-2:为什么学习Java Java是现在的主流1-3:Java可以做什么 ...
- jsp 内置对象(五)
1.Request对象 该对象封装了用户提交的信息,通过调用该对象相应的方法可以获取封装的信息,即使用该对象可以 获取用户提交的信息. 当Request对象获取客户提交的汉字字符时,会出现乱码问题,必 ...
- linux 播放加密DVDs
尝试下 https://www.cyberciti.biz/faq/howto-ubuntu-linux-playback-dvd/
- HTML(五)选择器--伪类选择器
HTML代码 <body> <a href="www.baidu.com">www.baidu.com</a> </body> CS ...
- delphi 演示数据路径
链接里默认的--------------------------- Error --------------------------- I/O error for file "C:\Prog ...
- RabbitMQ fanout类型的Exchange
就目前来说,Exchange是与消息发送端有关的,因为它可以指定将消息发送到哪个或哪些队列中. 本篇文章介绍的fanout类型就是指定将消息群发到与Exchange绑定的所有队列中. fanout这个 ...
- main.jsbundle 脱离掉本地服务
我们在本地调试的时候,可以使用index.js来开启本地服务,在局域网内运行app. 但是你会发现一旦你脱离了这个局域网就会造成app无法显示 这时候我们使用main.jsbundle 1.在Reac ...
- opencv3.0+vs2013安装记录
为了能够更好的学习图像,我觉得opencv是一个必不可少的库,因此在以后的研究上使用opencv作为研究工具,与大家共同进步. 话归正题:先搭建opencv的环境. 1.下载安装包3.0 a,官网打开 ...
- IEnumerable<T> list注意事项
方法返回的时候 要设置用list会比较稳妥. 遇到的问题: private IDbConnection GetConnection(){var dataSettingsManager = new Da ...
- Linux如何从零开始搭建rsync服务器(centOS6)
Step1:检查rsync是否已经安装 rmp -qa rsync 如果没有安装的话,通过yum install rsync -y Step2:给rsync服务添加本地用户,用于管理本地目录. u ...