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. HashMap的四种遍历方式

    package com.xt.map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; impor ...

  2. vs code 快捷键设置:选中字母可以快速全部转换为大写或小写

    文件--->首选项--->键盘快捷方式--->搜索:"大写"--->点击"转换为大写"左侧的加号,然后设置快捷键后按enter即可完成添 ...

  3. 题解 P2280 【[HNOI2003]激光炸弹】

    题目链接: https://www.luogu.org/problemnew/show/P2280 思路: 简单的二维前缀和,最后扫描一遍求 max(ans,f[i][j]+f[i-r][j-r]-f ...

  4. Oracle update 多字段更新

    一次性update多个字段 以student表为例: -- 创建学生表 create table student ( id number, name varchar2(40), age number, ...

  5. css强制换行显示省略号之显示两行后显示省略号

    1,首先来一个固定宽度,在一行显示,超出隐藏,显示省略号的样式 display:block; white-space:nowrap; overflow:hidden; text-overflow:el ...

  6. 【Day1】2.安装运行Python

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

  7. How to mount remote Windows shares

      Contents Required packages Basic method Better Method Even-better method Yet Another Even-better m ...

  8. git tag 重写

    有的时候我们想要在git的master分支中插入一个tag,这个时候就需要我们先删除一个不重要的tag,然后切到我们想要提交内容的地方,重新打tag. 例如:在master分支上修改提交,在commi ...

  9. 【计算机网络】网络地址转换NAT

    网络地址转换NAT 要知道到每个IP使能的设备都需要一个IP地址.以一个家庭为例,假设当地的ISP已为该家庭分配过一块地址,但是后期家庭中的智能设备增加(智能手机.电脑等),这些都需要IP地址才可上网 ...

  10. Java语言基础(9)

    1 方法(二) 1) 不带参数没有返回值的方法: 案例:Demo1 public class Demo1 { static void show(){ System.out.println(" ...