HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列)
Time Limit: 10000/5000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
|
【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. |
给定两串数组 : a[1], a[2], ...... , a[N], 和 b[1], b[2], ...... , b[M] (1 <= N <= 1000000, 1 <= M <= 10000)。你的任务是找到一个数字K使得a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]。如果存在多个K, 输出最小的那个。 |
|
【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]. |
输入的第一行是一个数字T 表示测试用例的数量。每个测试用例有三行。 第一行神两个数N和M (1 <= M <= 10000, 1 <= N <= 1000000)。 第二行有N个整数a[1], a[2], ...... , a[N]。 第三行有M个整数b[1], b[2], ...... , b[M]。 所有整数的范围都在 [-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. |
对于每个测试用例,输出一行上述K值。如果K不存在,则输出-1. |
|
【Sample Input - 输入样例】 |
【Sample Output - 输出样例】 |
|
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 |
6 -1 |
【题解】
KMP可解,初次匹配成功的时候结束即可。
【代码 C++】
#include <cstdio>
int a[], b[];
int aLen, bLen, next[] = { - };
void setNext_b(){
int i = , j = -;
while (i < bLen){
if (j == - || b[i] == b[j]) next[++i] = ++j;
else j = next[j];
}
}
int fid(){
int i = , j = ;
while (i < aLen){
if (j == - || a[i] == b[j]) ++i, ++j;
else j = next[j];
if (j == bLen) return i - j + ;
}
return -;
}
int main(){
int i, t;
scanf("%d", &t);
while (t--){
scanf("%d%d", &aLen, &bLen);
for (i = ; i < aLen; ++i) scanf("%d", &a[i]);
for (i = ; i < bLen; ++i) scanf("%d", &b[i]);
setNext_b();
printf("%d\n", fid());
}
return ;
}
HDU 1711 Number Sequence(数列)的更多相关文章
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- 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裸题,板子题,有坑点)
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 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1711 Number Sequence(字符串匹配)
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 ...
随机推荐
- vsftpd匿名用户只能上传不能下载
目的 搭建一台FTP服务器,供学生上传作业使用. 要求 1.学生可以直接访问FTP服务器,无需登录: 2.只能上传文件,不能创建文件夹.重命名.删除等: 3.不能下载文件(防抄袭): 4.不能在线查看 ...
- linux内核中jiffies的回绕问题【转】
本文转载自:http://blog.csdn.net/yuanlulu/article/details/6019862 ======================================== ...
- Backup: Array in Perl6
Array in Perl6 继承List,而List又继承Iterable,Positional,Cool ARRAY.pop ARRAY.shift ARRAY.push: VALUES ARRA ...
- CentOS 7 更新源 – 使用国内 163 yum 源
突然想起试试 Docker,在一台计算机上安装了 CentOS 7,准备开工,突然想起还需要做一件事情,更改源,不然安装肯定会很慢,网上搜索了一下,文章很多,但是会出一些问题,所以将自己的成功的日志写 ...
- wamp集成环境 开启rewrite伪静态支持
什么是伪静态 伪静态就是:动态网页通过重写URL的方法实现去掉动态网页的参数,但在实际的网页目录中并没有必要实现存在重写的页面. 伪静态的目的 最主要的就是迎合搜索引擎方便搜索引擎蜘蛛(Spider) ...
- PHP之分页类
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/6/22 * Time: 21:37 */ Class P ...
- python-day7 python内置模块 面向对象
1.configparser模块 configparser用于处理特定格式的文件,其本质上是利用open来操作文件 # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2 ...
- Linux文件描述符与打开文件之间的区别(转载)
转载请说明出处:http://blog.csdn.net/cywosp/article/details/38965239 1. 概述 在Linux系统中一切皆可以看成是文件,文件又可分为: ...
- vim退出
esc退出编辑模式 :wq 保存后退出vi :w 保存但不退出 :q 离开 vi :q! 若曾修改过档案,又不想储存,使用 ! 为强制离开不储存档案
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B
Description You are given a table consisting of n rows and m columns. Numbers in each row form a per ...