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] ...
随机推荐
- JavaScript采用append添加的元素错误
1.错误叙述性说明 于IE览器上: Uncaught HierarchyRequestError:Failed to excute 'appendChild' on 'Node':The new ch ...
- 金色的 SQL注意事项(1)
page(1-75) 最好是没有意义的主键字段,以方便未来的扩展. PS:主键,以后标书编码填错须要改的时候,关联表都须要跟着改.假设是一个无意义的自增字段是主键就无此原因. 主键最好不要设置为联合主 ...
- Linux内核的同步机制---自旋锁
自旋锁的思考:http://bbs.chinaunix.net/thread-2333160-1-1.html 近期在看宋宝华的<设备驱动开发具体解释>第二版.看到自旋锁的部分,有些疑惑. ...
- Spring官方网站的改版后下载
Spring官方网站改版很长一段时间后还没有找到直接下载Jar链接包,下面总结了一些方法,可在网上,亲測可用. 1.直接输入地址,改对应版本号就可以:http://repo.springsource. ...
- 原创游戏,金庸群侠传X 0.5公布
首先说一下背景,我个人从小特别爱玩游戏,对小时候一款游戏<金庸群侠传>DOS版更是情有独钟,自己工作以后,利用业余时间自己整了一个原创的改编版丢网上(找图片.音乐.写剧情更是虐心之极,耗时 ...
- 【hadoop之翊】——基于CentOS的hadoop2.4.0伪分布安装配置
今天总算是把hadoop2.4的整个开发环境弄好了,包括 windows7上eclipse连接hadoop,eclipse的配置和測试弄得烦躁的一逗比了~ 先上一张成功的图片,hadoop的伪分布式安 ...
- HDU 1420 Prepared for New Acmer【中国剩余定理】
/* 解决问题的思路:中国剩余定理,还要注意的是数据的类型,要使用__int64位 解决人:lingnichong 解决时间:2014-08-30 06:56:35 :简单题 */ Prepared ...
- 72_leetcode_Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...
- VS2012的变态优化,双循环变单循环
VS2010也是一样
- EasyUI基础searchbox&progressbar(搜索框,进度条)
easyui学习的基本组成部分(八个部分)硕果仅存searchbox和pargressbar.tooltip该,有一点兴奋.本文将偏向searchbox和pargressbar做一个探讨.鉴于双方的内 ...