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 ...
随机推荐
- Elasticsearch1.x 和Elasticsearch2.x 拼音分词插件lc-pinyin安装教程
Elasticsearch1.x 基于lc-pinyin和ik分词实现 中文.拼音.同义词搜索 https://blog.csdn.net/chennanymy/article/category/60 ...
- 【bzoj1797】[Ahoi2009]Mincut 最小割 网络流最小割+Tarjan
题目描述 给定一张图,对于每一条边询问:(1)是否存在割断该边的s-t最小割 (2)是否所有s-t最小割都割断该边 输入 第一行有4个正整数,依次为N,M,s和t.第2行到第(M+1)行每行3个正 整 ...
- LeetCode -- 3SumCloset
Question: Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- [CF912B]New Year's Eve
题意:在1~n中选不超过m个数,求其异或最大值 题解:经过找规律发现如果m为1,输出n,不然输出最小的不超过n的2^k-1 C++ Code: #include<cstdio> using ...
- [洛谷P3690]【模板】Link Cut Tree (动态树)
题目大意:给定$n$个点以及每个点的权值,要你处理接下来的$m$个操作.操作有$4$种.操作从$0到3编号.点从1到n编号. $0,x,y$:代表询问从$x$到$y$的路径上的点的权值的$xor$和. ...
- 洛谷 P2529 [SHOI2001]击鼓传花 解题报告
P2529 [SHOI2001]击鼓传花 题意:求出\(n!\)末尾最后一位非0数字 数据范围:\(n<=10^{100}\) 我们从简单的开始考虑 1.显然,\(n!\)可以被这么表示 \(n ...
- 安卓中使用iconfont
https://www.cnblogs.com/dongweiq/p/5730212.html
- ext4文件系统由文件的inode号定位其inode Table
在ubuntu中(以16.06为例),stat filename 可以查看文件的inode数值,但是如何确定该inode项具体在哪个块组下的inode Table中不是那么容易,接下来通过一步步计算来 ...
- 关于IE6的一些总结
开篇之前,循例简单说说IE6的一些背景吧. IE6是指微软浏览器系列中的第六个版本,它是在2001年的时候伴随着XP系统的问世而同时推出的一款浏览器.因为XP普及的原因,这款浏览器一度问鼎全球浏览器市 ...
- IE9,IE10 CSS因Mime类型不匹配而被忽略问题 (转)
写页面的时候在chrome,fireforks等页面上显示正常,但是换成IE9,IE10之后就完全没有样式了,报错信息是CSS 因 Mime 类型不匹配而被忽略,下面与大家分享下这个问题的相关的回答 ...