HDU 1711 Number Sequence(kmp)
If there are more than one K exist, output the smallest one.
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].
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
6
-1
还是kmp,就是把字符变成整型数组了。还是模板
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define N 1000005 int n,m;
int a[N],b[N];
int next[N]; void getfail(int *b)
{
int i,j;
next[0]=-1;
j=-1;
i=0; while(i<m)
if(j==-1||b[i]==b[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
} int kmp(int *a,int *b)
{
int i,j;
i=j=0;
while(i<n)
{
if(j==-1||a[i]==b[j])
{
i++;
j++;
}
else
j=next[j]; if(j==m)
return i-j+1;
}
return -1;
} int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m); for(i=0;i<n;i++)
scanf("%d",&a[i]); for(i=0;i<m;i++)
scanf("%d",&b[i]); getfail(b); printf("%d\n",kmp(a,b));
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
HDU 1711 Number Sequence(kmp)的更多相关文章
- HDU 1711 Number Sequence(KMP)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- HDU 1711 Number Sequence (KMP)
白书说这个是MP,没有对f 数组优化过,所以说KMP有点不准确 #include <stdio.h> int a,b; int T[1000010],P[10010];//从0开始存 in ...
- 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简单题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 1711 Number Sequence(kmp)
Number Sequence Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) To ...
- HDU 1711 Number Sequence(KMP模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...
- hdu 1711 Number Sequence(kmp找子串第一次出现的位置)
题意:裸kmp 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string.h> using nam ...
- hdu 1711 Number Sequence(KMP模板题)
我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...
- HDU 1711 Number Sequence(KMP匹配数字串)
这个就是kmp的数组形式,不用来处理字符串还真有点不习惯呢... #include<iostream> using namespace std; ,MAXM = ; int T[MAXN] ...
随机推荐
- linux下多进程的调试
linux下多进程的调试: (1)follow-fork-mode set follow-fork-mode [parent | child] ---- fork之后选择调试父进 ...
- 分享几个免费的开源邮件server软件
因为企业的须要,我们非常可能须要架设一个邮件server,微软的Exchange太复杂?GOOGLE出来的又收费!头大了吧,OK,贾芸斐在这里给大家分享推荐几个免费的开源的邮件server软件.希望你 ...
- HTML contact form with CAPTCHA
http://www.html-form-guide.com/contact-form/html-contact-form-captcha.html#codedownload
- javascript获取当前url中的參数
javascript获取当前页面url中的參数能够使用location的search方法,获取到的是url中?后面的部分,比如http:localhost:8080/Manager/index.jsp ...
- Javadoc的Html文件传输chm
Javadoc的Html文件转chm 工具下载地址:http://msdn.microsoft.com/en-us/library/ms669985.aspx 两篇相关文章: MyEclipse ...
- ajax j跨域请求sonp
需求 遇到的问题 解决方案 需求 如今,该项目需要获得数据访问外部链接.它是跨域.使用ajax 直提示: 遇到的问题 1. 怎样使用ajax 跨域请求数据 2. 能不能post请求 解决的方法 经过网 ...
- 【原创】纯OO:从设计到编码写一个FlappyBird (一)
说起来,自学计算机也有2年多的时间了,自己还没有从设计到编码,完完整整的设计一个基于面向对象的软件的经历..囧 于是,就有了这个系列.首先选用的语言是Java,没别的原因,HeadFirst设计模式是 ...
- rac 中节点的vip在该节点启动不了,在其它节点正常启动。
本实验是在虚拟机中! 与实验同样的问题:http://blog.csdn.net/wanghui5767260/article/details/38850335 问题描写叙述: 节点1.节点2和节点3 ...
- 解决adb server is out of date. killing...问题
在运行 adb 命令时出现了例如以下提示: adb server is out of date. killing... 导致 adb 无法正常启动,更无法运行其它命令. 有问题怎么办?百度呗.查了查 ...
- setsockopt角色
功能描写叙述: 获取或者设置与某个套接字关联的选 项. 选项可能存在于多层协议中.它们总会出如今最上面的套接字层. 当操作套接字选项时.选项位于的层和选项的名称必须给出.为了操作套接字层的选项,应该 ...