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  是一道模板题
不过还是wa了两发
1.不能开next数组   和系统自带名字重名
2.一开始我是把一个个数据转化成一条字符串来进行kmp    但是显然是错的QWQ
改成数组形式就行了
 
#include<bits/stdc++.h>
using namespace std;
//input
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);i--)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m);
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define inf 0x3f3f3f3f
#define REP(i,N) for(int i=0;i<(N);i++)
#define CLR(A,v) memset(A,v,sizeof A)
//////////////////////////////////
int nex[+];
int lenp,lens;
int p[+];//可以是char 也可以是string
int s[+];
void getnext()
{
nex[]=-;
int k=-,j=;
while(j<lenp-)
{
if(k==-||p[j]==p[k])
nex[++j]=++k;
else k=nex[k];
}
}
int kmp()
{
//lens=
//lenp=
int j=;
int i=;
while(i<lens&&j<lenp)
{
if(s[i]==p[j]||j==-)
{
i++;
j++;
}
else
j=nex[j]; if(j==lenp)
{
return i-j+;
}
}
return -;
}
int main()
{
int cas;
RI(cas);
while(cas--)
{
RII(lens,lenp);
rep(i,,lens-)
RI(s[i]);
rep(i,,lenp-)
RI(p[i]); getnext();
cout<<kmp()<<endl;
}
return ;
}
 
 
 

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 Number Sequence KMP 基础题

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

  3. HDU 1711 Number Sequence (KMP 入门)

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

  4. 1711 Number Sequence(kmp)

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

  5. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  6. hdu1711 Number Sequence kmp应用

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目: Problem Description Given two sequences of n ...

  7. Number Sequence (KMP的应用)

    个人心得:朴素代码绝对超时,所以要用到KMP算法,特意了解了,还是比较抽象,要多体会 Given two sequences of numbers : a11, a22, ...... , aNN, ...

  8. Number Sequence(KMP,判断子串 模板)

    题意: 给两数组,求一个是否是另一个的子数组,若是返回匹配的首位置 分析: KMP 入门 //扫描字符串A,并更新可以匹配到B的什么位置. #include <map> #include ...

  9. HDU 1711 Number Sequence KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...

随机推荐

  1. [转]TSVNCache.exe卡死电脑的解决方法

    转至于https://blog.csdn.net/gnail_oug/article/details/55506820. 正文如下: 每当打开explorer资源管理器的时候,经常卡死,换了固态硬盘还 ...

  2. Hystrix浅谈

    Hystrix如何使用很多说明,看了很多博客,却发现能说明一些简单概念的文章就没有. 所以本文不太回去说如何使用 Hystrix ,但是会简明的说一下 一些概念 super(Setter.withGr ...

  3. linux学习之netstat

    netstat -anp netstat -anp -a 表示展示所有 -p 展示进程和名称 -n --numeric don't resolve names

  4. 【进阶3-4期】深度解析bind原理、使用场景及模拟实现(转)

    这是我在公众号(高级前端进阶)看到的文章,现在做笔记  https://github.com/yygmind/blog/issues/23 bind() bind() 方法会创建一个新函数,当这个新函 ...

  5. 通过printf从目标板到调试器的输出

    最近在SEGGER的博客上看到Johannes Lask写的一篇关于在调试时使用printf函数从目标MCU输出信息到调试器的文章,自我感觉很有启发,特此翻译此文并推荐给各位同仁.当然限于个人水平,有 ...

  6. RCNN--目标检测

    原博文:http://www.cnblogs.com/soulmate1023/p/5530600.html 文章简要介绍RCNN的框架,主要包含: 原图-->候选区域生成-->对每个候选 ...

  7. hexo d 部署博客时出错

    问题描述: // 第一次遇到的问题 Error: packet_write_wait: Connection to 192.30.253.113 port 22: Broken pipe packet ...

  8. 新版Go2Shell 安装详解

    Go2Shell 下载地址 https://zipzapmac.com/Go2Shell 安装说明 1,首先下载好程序,然后运行到下面界面 2 然后将程序拖到下面位置 3,然后执行install Go ...

  9. PDF如何去除背景,PDF去除背景颜色

    PDF文件在使用的时候大多都是单调的白色背景,但是也有小伙伴再制作PDF文件的时候会给PDF文件添加背景颜色,会有影响文字阅读的情况,这个时候就需要把背景颜色去除了,那么该怎么做呢,不会的小伙们就跟小 ...

  10. python网络爬虫笔记(三)

    一.切片和迭代 1.列表生成式 2.生成器的generate,但是generate保存的是算法,所以可以迭代计算,没有必要,每次调用generate 二.iteration 循环 1.凡是作用于for ...