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. Android手机控制电脑撸出HelloWorld

    最近在开发一个远程办公的软件. 昨天在手机调通,并且成功通过手机打开电脑上的Eclipse撸出来一个HelloWorld. 也许不久的将来, 下班后,拿着手机在家写代码了.工作时间直接变成24/24 ...

  2. MSSQL → 04:表的创建与维护

    一.创建表 1.1.使用SQL Server Management Studio创建表 ①.打开刚刚建立的CollegeSystemDB数据库,找到表(数据库->CollegeSystemDB- ...

  3. k8s 超详细总结,面试必问

    一个目标:容器操作:两地三中心:四层服务发现:五种Pod共享资源:六个CNI常用插件:七层负载均衡:八种隔离维度:九个网络模型原则:十类IP地址:百级产品线:千级物理机:万级容器:相如无亿,K8s有亿 ...

  4. 外贸电子商务网站之Prestashop 安装后台中文语言包

    1.先进入到后台,我们进入Localization-> Localization2, 在下面的国家列表中,我们选择china ,导入即可. 3.进入Localization-> Trans ...

  5. 【转载】CPU相关总结

    What is the difference between Processor, Core, Logical Processor ? Processor : It’s the physical co ...

  6. 【时光回溯】【JZOJ3568】【GDKOI2014】小纪的作业题

    题目描述 输入 输出 有M行,每个询问一行,输出结果mod 1,000,000,007的值. 样例输入 10 3 3 5 1 2 3 1 3 5 2 1 7 9 3 9 2 3 样例输出 10 19 ...

  7. ios7.1安装提示"无法安装应用程序 由于证书无效"的解决方式二(dropbox被封项目转移到Appharbor上)

    6月18日起dropbox被天朝封了(这个真是无力吐槽),而ios7.1要求使用ssl安全连接,则须要又一次找到一个支持https的免费server. Appharbor是个不错的选择,操作简单.此外 ...

  8. java代码简单实现队列

    1. 基于链表简单实现 import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; / ...

  9. ModuleNotFoundError: No module named 'tools.nnwrap' pytorch 安装

    https://pytorch.org/get-started/locally/ pytorch 主页选择后安装

  10. 1x1卷积

    你可能会想为什么有人会用1x1卷积,因为它关注的不是一块像素,而是一个像素,图1 图1 我们看看传统的卷积,它基本上是运行在一个小块图像上的小分类器,但仅仅是个线性分类器.图2 图2 如果你在中间加一 ...