Number Sequence

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

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
 

算法:KMP匹配

#include <iostream>
#include <cstdio> using namespace std; const int maxn = 1e6+; int a[maxn];
int b[maxn];
int Next[maxn]; void getNext(int len) {
int i = , j = -;
Next[] = -;
while(i < len) {
while(j != - && b[i] != b[j]) {
j = Next[j];
}
Next[++i] = ++j;
}
} int main() {
int T;
scanf("%d", &T);
while(T--) {
int n, m;
scanf("%d %d", &n, &m);
for(int i = ; i < n; i++) {
scanf("%d", &a[i]);
}
for(int j = ; j < m; j++) {
scanf("%d", &b[j]);
}
getNext(m);
int i = , j = ;
int ans = -;
while(i < n) {
while(j != - && a[i] != b[j]) {
j = Next[j];
}
i++, j++;
if(j >= m) {
ans = i - j + ;
break;
}
}
printf("%d\n" ,ans);
}
return ;
}

HDU 1711:Number Sequence(KMP)的更多相关文章

  1. HDU 1711:Number Sequence(KMP模板,求位置)

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

  2. HDU - 1711 A - Number Sequence(kmp

    HDU - 1711 A - Number Sequence   Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...

  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)

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

  5. 1711 Number Sequence(kmp)

    Number Sequence Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) To ...

  6. Number Sequence(kmp)

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

  7. HDU 1711 Number Sequence (KMP)

    白书说这个是MP,没有对f 数组优化过,所以说KMP有点不准确 #include <stdio.h> int a,b; int T[1000010],P[10010];//从0开始存 in ...

  8. HDU 2062:Subset sequence(思维)

    Subset sequence Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. HDU 4763:Theme Section(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Problem Description   It's time for mus ...

随机推荐

  1. [多平台]pymo – 手机上的 GalGame 引擎

    [多平台]pymo – 手机上的 GalGame 引擎 介绍下这个能在手机上玩移植 GalGame 的游戏引擎,不知道有多少人听过呢?相信如果有喜欢在手机上玩 GalGame 的同学肯定听过类似的东西 ...

  2. 怎样理解 Vue 中的 v-if 和 v-show ?

    1. v-if 实现了真正的 条件渲染, 条件为真时, 节点被创建, 相应的监听函数也会生效, 条件为假时, 节点被销毁, 触发事件监听函数不会生效. 而 v-show 只是使用了 display:n ...

  3. docker_nginx_php_mysql

    https://blog.csdn.net/miwumuge/article/details/83386679 问题 1.容器内访问宿主机地址, host.docker.internal

  4. jquery选择器 模糊查找

    $("input[class^='combo-text']").attr("readonly", "readonly"); 查找包含‘com ...

  5. java实现spark常用算子之Reduce

    import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...

  6. nfs服务器的搭建和使用

    目录 更新记录 1.nfs介绍 1.1 nfs概念 1.2 nfs工作原理 1.3 nfs通讯过程 2.搭建和测试 NFS 服务器 2.1 搭建NFS服务器 2.2 测试NFS服务器 3.在线调试:N ...

  7. Myeclipse启动后tomcat空指针异常

    今天早上吃完早餐来公司上班,打开电脑,输入密码,123456.....嗯……,再打开myeclipse,duang...duang...duang....tomcat空指针异常,tmd我这暴脾气昨天还 ...

  8. jsonp的跨域原理

    在开发测试中,难免会在不同域下进行跨域操作,出于安全性考虑,浏览器中的同源策略阻止从一个域上加载的脚本获取或者操作 另一个域下的文档属性,这时需要进行跨域的方式进行解决,如:使用jsonp ,ifra ...

  9. Js中去除数组中重复元素的6种方法

    方法一: Array.prototype.method1 = function(){ var arr=[]; //定义一个临时数组 for(var i = 0; i < this.length; ...

  10. 【Day2】2.函数

     视频地址(全部) https://edu.csdn.net/course/detail/26057 课件地址(全部) https://download.csdn.net/download/gentl ...