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. IntelliJ IDEA Spring boot devtools 实现热部署

    一.spring-boot-devtools是一个为开发者服务的一个模块,其中最重要的功能就是自动部署新代码. 二.原理 使用了两个ClassLoader,一个ClassLoader用来加载那些不会变 ...

  2. “007~ASP 0104~不允许操作”错误的解决方法(图解)

    今天测试一个Z-Blog程序的上传文件时发现总提示“ 007~ASP 0104~不允许操作 ”的错误,经过度度上各位朋友的帮忙,终于找到解决方法. 这是windows2003 server对上传文件的 ...

  3. 如何将编译后的文件打包成jar文件

    如果需要修改像spring和dubbo中的jar包源码,修改后怎么打包呢? 如下: 1.win+r进入命令行: 2.找到需要打包的class文件: 3.jar -cvf [jar包的名字] [需要打包 ...

  4. mui APP 微信登录授权

    一.在微信平台上申请appid.appsecret. 二.app --> manifest.json-->SDK配置(填写申请好的appid和appsecret) 三.在登录页,点击微信登 ...

  5. vue-无限滚动

    <ul class="infinite-list" v-infinite-scroll="load" style="overflow:auto& ...

  6. kbmMemTable中怎么根据UniqueRecID定位到对应的记录

    function TForm5.LocateUniqueRecID(aDataSet: TkbmMWCustomClientQuery; AID: TkbmNativeInt): Boolean; v ...

  7. selenium:能够模拟人类打开浏览器的爬虫利器

    介绍 selenium相当于是一个机器人,可以模拟人类登陆浏览器的行为,比如点击.填充数据.删除cookie等等.Chromedriver是一个驱动Chrome的程序,使用它才可以驱动浏览器,其实Ch ...

  8. Servlet登录小案例

    需求:登录功能 登录页面输入用户名和密码, 到数据库进行验证 ,如果成功跳转到success.html页面,失败跳转到error.html页面数据库 mysql,数据表 t_user表[表中的字段 : ...

  9. 8.1.Zookeeper概念简介

    1.分布式系统概述 理解1: 分布式系统:分布式系统是针对一个大系统而言,将一个大系统分成多个子系统,即多个工程系统. 我们先看下传统的系统模式:    传统的系统模式将多个功能模块全部在一个工程中写 ...

  10. Hive的视图和索引(九)

    Hive的视图和索引 1.Hive Lateral View 1.基本介绍 ​ Lateral View用于和UDTF函数(explode.split)结合来使用. ​ 首先通过UDTF函数拆分成多行 ...