个人心得:朴素代码绝对超时,所以要用到KMP算法,特意了解了,还是比较抽象,要多体会

Given two sequences of numbers : a11, a22, ...... , aNN, and b11, b22, ...... , bMM (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make aKK = b11, aK+1K+1 = b22, ...... , aK+M−1K+M−1 = bMM. If there are more than one K exist, output the smallest one. 

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的应用)的更多相关文章

  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. Number Sequence kmp

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

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

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

  7. hdu1711 Number Sequence kmp应用

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

  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. range精讲

    var ec = range.endContainer endContainer不是一个引用类型 range是引用类型 range经过改变范围之后 var ec2 =range.endContaine ...

  2. iOS Swift 熊猫🐼跑酷 第一个小项目

    前言:想用swift  写个小游戏 慢慢转化 能写出 ARKit来.但是又不能一口吃个胖子,慢慢来,在网络视频教程中撸了视频教学,断断续续看了半个多月,基本实现了 游戏主角

  3. Swift进阶 - 12个技巧

    听说你已经学习Swift几个月了,有没有想更进一步成为Swift高手的想法?我这里有11招秘技,各位施主且听我慢慢道来,结个善缘. 1. 扩展(Extension) 任务: 求数字的平方. // 菜鸟 ...

  4. ORA-28002 the password will expire

    ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

  5. 树莓派使用DHT11温湿度传感器(C语言程序)

    pi4j是基于wiringpi开发的通过java来控制树莓派GPIO口的库文件.在java程序中引入相关类就可以使用已经封装好的方法控制树莓派GPIO口. pi4j官网:http://pi4j.com ...

  6. 主攻ASP.NET MVC4.0之重生:使用反射获取Controller的ActionResult

    示例代码 public ActionResult TypeOfForName() { Type typeinfo = typeof(CustomerClassController); //typeof ...

  7. tornado解析 第一篇

    一.tornado介绍 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 we ...

  8. 未能将网站配置为使用ASP.NET4.X 解决方法

    WIN 10系统安装Visual Studio 2012新建ASP.NET MVC 4 WEB 应用程序出错 有些图片是网上截取而来,之前光顾着处理问题而忘记截图了,提示的ASP.net 版本有些不同 ...

  9. hive学习2(Navicat连接hive)

    Navicat连接hive 第一步:win下安装好mysql 第二步:win下安装Navicat 第三步:启动hadoop集群,启动hive 第四步:Navicat连接hive 在第四步中需先配置ss ...

  10. ssh学习(1)

    雇员薪资管理系统(crud) ①先搞定spring ②引入spring包 ③编写applicationContext.xml文件(或者beans.xml),我们把该文件放在src目录下 ④测试一下sp ...