Number Sequence

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
 
题目大意:给定两个序列a,b,求出b序列在a序列中第一次出现的位置,若不在a序列中输出-1;
思路:KMP模板直接套用,这里觉得这位博主写的挺通俗易懂的还是很简单的就看懂了https://blog.csdn.net/starstar1992/article/details/54913261/
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<string> using namespace std;
const int maxn = ;
int rs[][], nex[maxn], a[maxn], b[maxn/ + ], n, m;
void cal_next(int *str, int len)
{
nex[] = -; int k = -;
for (int i = ; i <= len - ; i++) {
while (k > - && str[k + ] != str[i])
k = nex[k];
if (str[k + ] == str[i])k = k + ;
nex[i] = k;
}
}
int KMP(int *a, int la, int *b, int lb)
{
cal_next(b , lb);
int k = -;
for (int i = ; i < la; i++) {
while (k > - && b[k + ] != a[i])
k = nex[k];
if (b[k + ] == a[i])k = k + ;
if (k == lb - )return i - lb + ;
}
return -;
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
cin >> n >> m;
for (int i = ; i < n; i++)cin >> a[i];
for (int i = ; i < m; i++)cin >> b[i];
int ans = KMP(a, n, b, m);
if (ans != -)cout << ans + << endl;
else cout << "-1" << endl;
}
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 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模板题]

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

  4. HDU 1711 Number Sequence KMP

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

  5. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  6. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  7. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

  8. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  9. HDU 1711 Number Sequence (KMP简单题)

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

随机推荐

  1. 带三角形下标的提示框(按钮button)

    HTML:<div class="leaflet-popup-content-wrapper"> <div class="leaflet-popup-c ...

  2. Nginx教程(五) Nginx配置文件详解 (转)

    一. Nginx配置文件nginx.conf中文详解 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processe ...

  3. struts1之工作原理

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zkn_CS_DN_2013/article/details/34452341 1.初始化:strut ...

  4. Framework7 无限滚动

    html结构 <div class="page"> <div class="page-content infinite-scroll"> ...

  5. [ITOO]动态建库 标签: 库数据库mysql 2016-07-17 21:23 241人阅读 评论(2) 收

    最近一直在做权限系统的动态建库,动态建库,说白了就是在你点击"注册"按钮的时候,根据你输入的信息,来创建一个企业所需要的数据库的过程,因为现阶段并没有提供购买等功能,所以暂时咱们是 ...

  6. @bzoj - 4298@ [ONTAK2015]Bajtocja

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定d张无向图,每张图都有n个点.一开始,在任何一张图中都没有任 ...

  7. xcode禁用代码分析的警告和内存泄漏

    在使用xcode进行iphone应用开发时,经常需要添加一些第三方的类库,而一些第三方的类库由于缺少维护,从而导致类库中含有各种警告和各种内存泄漏,但并不影响运行. 倘若我们需要用到第三方库,而由不想 ...

  8. mysql的root密码忘记解决方

    mysql的root密码忘记解决方 没关注第一步,直接从第二步开始,(可以参看上一篇,先停止mysql服务).然后从第二步开始. 我启动mysql用的命令是/etc/init.d/mysql  sta ...

  9. C++:只用初始化列表初始化变量的几种情况

    1.类成员函数中const变量的初始化(也就是第一点) 有几个容易混淆的地方: (1)const 的变量只能通过构造函数的初始化列表进行初始化:(貌似在C++11中可以正常编译) (2)static ...

  10. Hbase数据模型 行