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 分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...
随机推荐
- jenkins多个slave遇到的坑
jenkins配置如下: 在Jenkins上添加了两个节点(Slave Node),且为这两个节点设置了一个相同的标签"windows".创建了一个新Job – "tes ...
- Java中 break continue return 的区别
1.break break :跳出当前循环:但是如果是嵌套循环,则只能跳出当前的这一层循环,只有逐层break才能跳出所有循环: ; i < ; i++) { ) { break; // 在执行 ...
- 牛客多校第十场-D- Rikka with Prefix Sum
链接:https://www.nowcoder.com/acm/contest/148/D来源:牛客网 Prefix Sum is a useful trick in data structure p ...
- 从匿名函数(闭包特性)到 PHP 设计模式之容器模式
匿名函数(匿名函数) 匿名函数,也叫闭包函数,它允许临时创建一个没有指定名称的函数,常用作回调函数参数的值,也可以作为变量的值来使用.具体的使用见以下示例代码: /* 示例一:声明一个简单匿名函数,并 ...
- spring集成ActiveMQ居然要依赖这么多包
做spring和ActiveMQ的集成,作maven依赖的时候有感(以前都不在乎,现在不一样了........省略) <!-- https://mvnrepository.com/artifac ...
- 用贪心算法近似求解 Loading Balance 问题(作业调度的负载均衡)
一,Loading Balance 问题描述:有 m 台相同的机器及 n 个作业,其中 m={M(1),M(2),……M(m)}.n = {J(1),J(2),……J(n)}.每个作业都有一个处理时间 ...
- STM32建立基本项目
1.下载固件库解压 2.Keil建立项目,项目下建立三个文件夹3.各目录下从固件库中拷贝对应的目录 4.修改main.c文件 5.修改C++ 6.修改Include 7,编译.
- 12. SpringBoot国际化
1).编写国际化配置文件: 2).使用ResourceBundleMessageSource管理国际化资源文件 3).在页面使用fmt:message取出国际化内容 步骤:1).编写国际化配置文件,抽 ...
- 第二节--Servlet
1.在tomcat的webapp下新建一个web项目test 要有WEB-INF目录,其下有web.xml. 2.WEB-INF下文件是给tomcat使用的 3.用户访问localhost:848 ...
- Linux - find 查找
# linux文件无创建时间 # Access 使用时间 # Modify 内容修改时间 # Change 状态改变时间(权限.属主) # 时间默认以24小时为单位,当前时间到向前24小时为0天,向前 ...