HDU1711 Number Sequence(KMP模板题)
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 40053 Accepted Submission(s): 16510
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
#include<iostream>
#include<string.h>
using namespace std;
int tt,pp;
int t[1000011],p[11111],next1[11111];
void getnext()
{
int i=0,j=-1;
next1[0]=-1;
while(i<pp-1)
{
if(j==-1||p[i]==p[j])
next1[++i]=++j;
else
j=next1[j];
}
return;
}
int kmp()
{
int i=0,j=0;
while(i<tt)
{
if(j==-1||t[i]==p[j])
i++,j++;
else
j=next1[j];
if(j==pp)
return i-j+1;
}
return -1;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&tt,&pp);
for(int i=0;i<tt;i++)
scanf("%d",&t[i]);
for(int i=0;i<pp;i++)
scanf("%d",&p[i]);
getnext();
printf("%d\n",kmp());
}
return 0;
}
HDU1711 Number Sequence(KMP模板题)的更多相关文章
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- hdu1711 Number Sequence kmp模板
题目传送门 学习博客 学习了kmp算法,理解了算法思想,但还没有到能把这个思想用语言来描述出来. #include<bits/stdc++.h> #define clr(a,b) mems ...
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU1711 Number Sequence KMP
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU1711 题意概括 给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出 ...
- hdu 1711 Number Sequence KMP 基础题
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu1711 Number Sequence kmp应用
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目: Problem Description Given two sequences of n ...
- POJ Oulipo KMP 模板题
http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4 ...
- POJ Oulipo(KMP模板题)
题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...
- Number Sequence - HDU 1711(KMP模板题)
题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...
随机推荐
- 5、JDBC-元信息
DatabaseMetaData:描述数据库的元数据对象 获取所有数据库 import org.junit.jupiter.api.AfterEach; import org.junit.jupite ...
- HDU - 5413 CRB and Roads
CRB and Roads Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 在windows中停止mysql提示:'服务正在启动或停止中,请稍候片刻后再试一次'
发现mysql的windows服务异常,准备卸载并重新注册服务,输入: mysqld --remove MySQL 提示: 发现卸载不掉这个服务,于是找到MySQL服务的pid sc queryex ...
- 选择排序算法的JAVA实现
1,采用选择排序对元素进行排列时,元素之间需要进行比较,因此需要实现Comparable<T>接口.即,<T extends Comparable<T>>. 更进一 ...
- pyqt5-键盘事件
视频教程:https://v.qq.com/x/page/p08592bhsag.html keyPressEvent(QKeyEvent) 键盘按下时调用 keyReleaseEvent(QK ...
- OpenCV入门(2)- Java第一个程序
1.下载和安装OpenVC环境 从官方连接 https://opencv.org/releases.html 下载Windows的安装包 下载下来的就是一个压缩包,安装解压后,对Java开发有效的目录 ...
- CF101D Castle
传送门 首先,一定要把所有点遍历一遍,这时答案应该是\(\frac{\sum 某个点第一次被遍历的时间点}{n-1}\quad\),而且每条边只能走两次,所以每次要遍历完某棵子树才能遍历其它子树. 考 ...
- c#将前端传来的Json解析成对象
描述:因工作中需要将C#中的Json字符串转换为对象,对此记录下. 解决办法: 1.前端传过来的Json字符串,OrderAppModuleJson即前端传递到后端的Json字符串 string st ...
- c#中富文本编辑器Simditor带图片上传的全部过程(项目不是mvc架构)
描述:最近c#项目中使用富文本编辑器Simditor,记录一下以便以后查看. 注:此项目不是MVC架构的. 1.引用文件 项目中引用相应的css和js文件,注意顺序不能打乱,否则富文本编辑器不会正常显 ...
- Service的线程、工作线程、权限及系统Service
Service组件和其他组件一样,都是运行于应用的主线程当中,它们都运行于同一个单一的线程中. 可以把Service简单的理解成一个没有界面显示的Activity(这个比喻其实并不准确,因为Servi ...