Number Sequence (KMP的应用)
个人心得:朴素代码绝对超时,所以要用到KMP算法,特意了解了,还是比较抽象,要多体会
InputThe 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 a11, a22, ...... , aNN. The third line contains M integers which indicate b11, b22, ...... , bMM. All integers are in the range of −1000000,1000000−1000000,1000000.
OutputFor 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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<utility>
#include<queue>
#include<set>
using namespace std;
int n,m;
int a[],b[];
void getnext(int x[],int next[])
{
int i=;
int j=-;
next[i]=-;
while(i<m)
{
if(j==-||x[i]==x[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int next[];
int flag=-;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<m;i++)
scanf("%d",&b[i]);
getnext(b,next);
int i=,j=;
while(i<=n&&j<m)
{
if(j==-||a[i]==b[j])
{
i++;
j++;
}
else
j=next[j];
}
if(j==m) flag=i-j;
cout<<flag<<endl; }
return ; }
Number Sequence (KMP的应用)的更多相关文章
- 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 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...
- 1711 Number Sequence(kmp)
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- Number Sequence kmp
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- hdu1711 Number Sequence kmp应用
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目: Problem Description Given two sequences of n ...
- Number Sequence(KMP,判断子串 模板)
题意: 给两数组,求一个是否是另一个的子数组,若是返回匹配的首位置 分析: KMP 入门 //扫描字符串A,并更新可以匹配到B的什么位置. #include <map> #include ...
- HDU 1711 Number Sequence KMP
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...
随机推荐
- Service degrade
服务降级:主要是针对非正常情况下的应急服务措施;比如电商平台,在针对618.双11等高峰情形下采用部分服务不出现或者延时出现的情形. 为什么是非正常情况下呢,比如我要出差,如果经常出差的话,要带的衣服 ...
- Struts2笔记03——架构(转)
原始内容:https://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm 架构(很重要!尤其是图!) 从一个比较高的层次来看,St ...
- Dual Boot WINDOWS 10 and KALI LINUX Easily STEP BY STEP GUIDE截图
mark. kali安装:https://www.youtube.com/watch?v=KLj2yQPWZDk 删除无用分区:http://www.xitongcheng.com/jiaocheng ...
- 【Flask】Flask-Sqlalchemy使用笔记
### 安装:```shellpip install flask-sqlalchemy``` ### 数据库连接:1. 跟sqlalchemy一样,定义好数据库连接字符串DB_URI.2. 将这个定义 ...
- 初识python---简介,简单的for,while&if
一编程语言:编程语言是程序员与计算机沟通的介质: 编程语言的分类: 1机器语言:是用二进制代码表示的计算机能直接识别和执行的一种机器指令的集合. 优点:灵活,直接执行和速度快 ...
- 基于SSM的单点登陆03
TbUser.java和TbUserExample.java,TbUserMapper.java,TbUserMapper.xml由mybatis框架生成. generatorConfig.xml & ...
- 常用display属性
table 1. 宽高由内容撑开 2.独占一行 3.可设置宽高 4.可设置margin.padding inline-table 与display: table大体一致,区别在于不独占一行,为行内元素 ...
- 5.1深入理解计算机系统——系统级I/O
一.UNIX I/O 在UNIX系统中有一个说法,一切皆文件.所有的I/O设备,如网络.磁盘都被模型化为文件,而所有的输入和输出都被当做对相应文件的读和写来执行.这种将设备映射为文件的方式,允 ...
- Vue.js学习笔记 第一篇 数据绑定
双花括号文本插值 先来个最简单的例子,看完之后立马会用Vue了,是不是很有成就感 <!DOCTYPE html> <html> <head> <meta ch ...
- vRO 添加已有磁盘到VM
在vRO实现将已有虚拟机磁盘添加到另外的虚拟机上,以为vRA发布Oracle/SQL集群做准备: // 脚本需要两个输入 vm_obj和diskPathSystem.log("Attempt ...