Number Sequence - HDU 1711(KMP模板题)
题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1
#include<stdio.h>
#include<string.h> const int MAXM = 1e4+;
const int MAXN = 1e6+; int a[MAXN], b[MAXM], next_b[MAXM]; void GetNext(int b[], int next[], int M)
{
int i=, j=-;
next[] = -; while(i < M)
{
if(j==- || b[i]==b[j])
next[++i] = ++j;
else
j = next[j];
}
}
int KMP(int a[], int b[], int next[], int N, int M)
{
int i = , j = ; while(i < N)
{
while(j==- || a[i] == b[j] && j<M)
i++, j++; if(j == M)return i-M+; j = next[j];
} return -;
} int main()
{
int T; scanf("%d", &T); while(T--)
{
int i, N, M; scanf("%d%d", &N, &M); for(i=; i<N; i++)
scanf("%d", &a[i]);
for(i=; i<M; i++)
scanf("%d", &b[i]); GetNext(b, next_b, M); printf("%d\n", KMP(a, b, next_b, N, M));
} return ;
}
Number Sequence - HDU 1711(KMP模板题)的更多相关文章
- Number Sequence HDU 1711 KMP 模板
题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...
- (KMP 模板)Number Sequence -- Hdu -- 1711
http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...
- AC日记——Number Sequence hdu 1711
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Number Sequence HDU 1711(KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...
- HDU 2087 kmp模板题
s为主串 t为模板串 求t的nextt 加const #include<stdio.h> #include<string.h> #include<algorithm> ...
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- POJ Oulipo KMP 模板题
http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4 ...
- hdu 1686 KMP模板
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...
- Oulipo HDU 1686 KMP模板
题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...
随机推荐
- 【原创】FAT32系统硬盘的数据恢复实例,这篇文章也是从自己QQ空间的,转移过来的
FAT32系统硬盘的数据恢复实例软件:win hex(16进制的编辑器)恢复内容:在FAT32中恢复一张图片1.首先我们格式化一个分区为空,让分区的系统选为FAT32系统格式化.如下: psb.jpg ...
- HTML DOM节点
在 DOM 树中,基本上一切都是节点.每个元素在最底层上都是 DOM 树中的节点.每个属性都是节点.每段文本都是节点.甚至注释.特殊字符(如版权符号 ©).DOCTYPE 声明(如果 HTML 或者 ...
- 编写利用Fragment创建新闻列表
编写利用Fragment创建新闻列表 1.创建新闻实体类News,代码如下: public class News { private String title; private String co ...
- android 中Log - 简单使用
例如,我们可以使用'Log.d'进行Debug,在java代码中输入Log.d(String tag, String message),tag为自己命名的tag,message为待输出的信息.然后打开 ...
- cxf webservice异步调用
http://blog.csdn.net/changpingchen/article/details/9048347 http://www.oschina.net/question/780719_12 ...
- Log4Net使用指南(转)
转自:http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html 声明:本文内容主要译自Nauman Leghari的Using log4 ...
- 用jquery修改默认的单选框radio或者复选框checkbox选择框样式
默认的radio和checkbox选框很难看.我去看了一下qq注册的页面.发现单选和复选框并没有用<input>,居然是用是A标签.然后用css背景图片展示选择框,用JavaScript控 ...
- MapReduce概念(转)
昨天,我在Xebia印度办公室发表了一个关于MapReduce的演说.演说进行得很顺利,听众们都能够理解MapReduce的概念(根据他们的反馈).我成功地向技术听众们(主要是Java程序员,一些Fl ...
- javascript获得给定日期的前一天的日期
/** * 获得当前日期的前一天 */ function getYestoday(date){ var yesterday_milliseconds=date.getTime()-1000*60*60 ...
- 正则如何匹配div下的所有<li>标签?
<?php header('Content-Type:text/html;charset=utf-8'); $str = '<div class="c1s"> & ...