HDU 1711 Number Sequence【kmp求子串首次出现的位置】
InputThe 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].
OutputFor 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<stdio.h>
int a[],b[];
int next[];
int n,m;
void getNext()
{
int j,k;
j=;
k=-;
next[]=-;
while(j<m)
{
if(k==-||b[j]==b[k])
next[++j]=++k;
else k=next[k];
}
}
//返回首次出现的位置
int KMP_Index()
{
int i=,j=;
getNext(); while(i<n && j<m)
{
if(j==-||a[i]==b[j])
{
i++;
j++;
}
else j=next[j]; }
if(j==m) return i-m+;
else return -;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
for(int i=;i<m;i++)
scanf("%d",&b[i]);
printf("%d\n",KMP_Index());
}
return ;
}
kuangbin模板
http://www.cnblogs.com/kuangbin/archive/2012/08/14/2638803.html
HDU 1711 Number Sequence【kmp求子串首次出现的位置】的更多相关文章
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1711 Number Sequence KMP 基础题
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1711 Number Sequence (KMP 入门)
Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- HDU 1711 Number Sequence KMP
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...
- hdu 1711 Number Sequence(kmp找子串第一次出现的位置)
题意:裸kmp 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string.h> using nam ...
随机推荐
- Python 日期时间相关
#calendar日历模块 import calendar #calendar() 获取一年日历 result = calendar.calendar(2018,w = 2,l = 1,c = 6,m ...
- [Leetcode] unique paths ii 独特路径
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- BZOJ2753 [SCOI2012]滑雪与时间胶囊 【kruskal】
题目链接 BZOJ2753 题解 完了我连\(kruskal\)裸题都做不出来了.. 题目是求最小树形图,即有向图最小生成树 我们不能直接上\(kruskal\),而要保证先加入前面的点, 所以我们排 ...
- CSS3不遥远,几个特性你要知道
CSS是众所周知且应用广泛的网站样式语言,在它的版本三(CSS3)计划中,新增了一些能够节省时间的特性.尽管只有当前最新了浏览器版本才能支持这些 效果,但了解它们还是必须且很有趣味性的.CSS3中的5 ...
- mybatis学习(七)——resultType解析
resultType是sql映射文件中定义返回值类型,返回值有基本类型,对象类型,List类型,Map类型等.现总结一下再解释 总结: resultType: 1.基本类型 :resultType= ...
- Document base D:\devTools\apache-tomcat-6.0.51\webapps\AppService does not exist or is not a readable directory
tomcat通过eclipse发布项目到webapp后 手动删除在webapp目录下的文件,启动tomcat时,会报出异常找不到那个删除的项目. 解决方法是(1)重新发布项目到webapp (2)在 ...
- vue双向绑定原理
要了解vue的双向绑定原理,首先得了解Object.defineProperty()方法,因为访问器属性是对象中的一种特殊属性,它不能直接在对象中设置,而必须通过 Object.definePrope ...
- commons
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --> <dependency> <g ...
- hibernate连接oracle数据库
前言:以下所有的操作都是基于你已经成功安装了oracle数据库并且java的开发环境正常的情况下进行的. 如果没有完善请先配置基础环境. 第一步:配置需要的环境(下载并导入需要的包). 下载链接:ht ...
- Topcoder SRM 607 div1题解
好久没来写了,继续继续... Easy(250pts): //前方请注意,样例中带有zyz,高能预警... 题目大意:给你一个字符串,中间有一些是未知字符,请你求出这个字符串的回文子串个数的期望值.数 ...