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(数列)的更多相关文章

  1. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  2. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

  3. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  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 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. KMP - HDU 1711 Number Sequence

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 1711 Number Sequence(字符串匹配)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. HDU 1711 Number Sequence (KMP 入门)

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

随机推荐

  1. EL表达式,JSTL:jsp standard Tag Library

    1.EL表达式的作用: 1.1访问Bean的属性.  方式一:${对象名 . 属性名} eg:${user.name}    方式二:${对象名["属性名"]} 1.2输出简单的运 ...

  2. docker RESTful API

    https://docs.docker.com/engine/reference/api/docker_remote_api/

  3. Linux, Mac下Shell 数组 Array 的修理工

    我的测试基本都是在Mac,及Unix环境下测试的,如无特别注明,默认就是Mac 不论你看到这篇随笔是被shell array的奇淫巧技,还是发现shell array就在一对{}里面就可以做那么多勾当 ...

  4. 图解 交集(join)和 合并(union)

    假设我们有两张表. Table A 是左边的表. Table B 是右边的表. 其各有四条记录,其中有两条记录是相同的,如下所示: id name       id  name 1  Pirate   ...

  5. UIView完全置顶的方法

    一般来说,若需要独立添加一个UIView,使其覆盖于整个应用窗口之上,是这样实现的: AppDelegate *app = (AppDelegate *)[[UIApplication sharedA ...

  6. 用Jquery控制文本框只能输入数字和字母

    用Jquery控制文本框只能输入数字和字母 $.fn.onlyNum = function () { $(this).keypress(function (event) { var eventObj ...

  7. settings.xml

    <settings> <!--本地仓库.该值表示构建系统本地仓库的路径.其默认值为~/.m2/repository,windows:C:/Users/Administrator/.m ...

  8. oracle数据库表空间文件收缩实例

    Oracle数据文件收缩实例 数据文件的作用 HWM的基本概念 查看数据文件的使用情况 包括内容:数据文件大小,已经used空间,free空间,hwm信息 select /*+ ordered use ...

  9. sass初步认识1

    sass是一种“css预处理器”,同类的还有less等,方法类似.css预处理器的基本思想是,用一种专门的编程语言,进行网页样式设计,然后再编译成正常的css文件. 使用sass需要先暗转RUBY,再 ...

  10. jdk 与jre的区别

    jdk就是java的开发工具集,顾名思义就是你做开发用的,其中包括javac,也就是java compiler等. jre(java runtime environment),就是java程序的运行环 ...