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 日记(3) - 详解依赖注入 (转)
关于DI 依赖注入, 转载这篇文章, 写得很好的. ----------------------------- DI在.NET Core里面被提到了一个非常重要的位置, 这篇文章主要再给大家普及一下关 ...
- Java 正则校验整数,且小数点只能是2位
//金额验证 public static boolean isNumber(String str){ Pattern pattern=Pattern.compile("^(([1-9]{1} ...
- VS2010安装项目程序打包操作详解
(转自:http://blog.sina.com.cn/s/blog_74f702e60101at62.html) 1.打开VS2010,选择 新建项目---其他项目类型---Visual Studi ...
- java倒计时使用ScheduledExecutor实现,使用两个线程,以秒为单位
public class Countdown2 { private volatile int lin; private int curSec; public Countdown2(int lin) t ...
- bzoj1045
题解: 随便推一下公式 然后发现是中位数 代码: #include<bits/stdc++.h> using namespace std; ],n; long long sum; int ...
- Linux下IP的存储位置
在linux下,配置多个IP的话,通常是eth0... eth0. ..eth0.x等等, 那么如果要配置大量的IP呢,这么配置也是可以的,但是繁琐,虽说这种情况很少. 对于添加大量IP,有一定规定的 ...
- [Leetcode221]最大面积 Maximal Square
[题目] Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...
- IDEA中自动生成serialVersionUID
File >> Setting >> Inspections >> serializable 勾选上后,光标放在实现Serializable接口的类名上 ...
- Docker(2):快速入门及常用命令
什么是Docker? Docker 是世界领先的软件容器平台.开发人员利用 Docker 可以消除协作编码时“在我的机器上可正常工作”的问题.运维人员利用 Docker 可以在隔离容器中并行运行和管理 ...
- Aizu - 2681(括号匹配)
Problem Statement You are given nn strings str1,str2,…,strnstr1,str2,…,strn, each consisting of ( an ...