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. 【JZOJ4788】【NOIP2016提高A组模拟9.17】序列

    题目描述 输入 输出 样例输入 1 5 2 1 3 0 3 2 2 0 1 0 样例输出 1 数据范围 解法 考虑没有模的情况,问题就仅仅只是简单的差分问题(广告铺设): 设r[i]是第i位需要加的次 ...

  2. python 通过.pth文件修改搜索路径

  3. 跟我一起认识axure(三)

    交互设置,添加链接 点击预览

  4. MongoDB sharding 集合不分片性能更高?

    最近云上用户用户遇到一个 sharding 集群性能问题的疑惑,比较有代表性,简单分享一下 测试配置 mongos x 2.shard x 3 测试1:集合不开启分片,批量 insert 导入数据,每 ...

  5. 比较全面的一个PHP缓存类解析

    转自:http://www.blhere.com/1164.html 一.引论 PHP,一门最近几年兴起的web设计脚本语言,由于它的强大和可伸缩性,近几年来得到长足的发展,php相比传统的asp网站 ...

  6. Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix

    版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处.谢谢. https://blog.csdn.net/testcs_dn/article/details/ ...

  7. apply( )与 call( ) 的区别

    JavaScript中的每一个Function对象都有一个apply()方法和一个call()方法 语法 /*apply()方法*/ function.apply(thisObj[, argArray ...

  8. mysql列转行 行转列

    列转行 SELECT flag ,substring_index(substring_index(t.context,), ) as result FROM ( select 'aa' as flag ...

  9. c++ 模板和traits

    #define TEST(ITEMNAME) AddItem(ITEMNAME, #ITEMNAME); template <typename T> void AddItem(T& ...

  10. Git 的两种忽略文件方式 gitignore 和 exclude

    Git 的两种忽略文件方式 gitignore 和 exclude .gitignore 不用说了,大家都知道. 有一个 exclude 可能接触比较少. 知道这个功能后发现,用在服务器上非常方便,因 ...