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 ...
随机推荐
- Centos ssh 登陆乱码解决办法
1.vi /etc/sysconfig/i18n 将内容改为 LANG="zh_CN.GB18030"LANGUAGE="zh_CN.GB18030:zh_CN.GB23 ...
- C#—委托分析
1.简单委托示例 using System; using System.Collections.Generic; using System.Linq; using System.Text; names ...
- (转)PHP正则表达式的快速学习方法
1.入门简介 简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或 ...
- cxf WebService整理 (基于注解)
http://blog.csdn.net/zjw10wei321/article/details/39889823
- 如果使用的是orm,是否还需要关系索引
简而言之:是的,仍然需要理解索引,即使是使用对象关系映射(ORM)工具. ORM工具能够产生符合逻辑的,合法的查询(多数的时候),除非只是生成非常基本的查询(例如仅是根据主键查询的),否则它很难生成适 ...
- 文件上传-html
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>文件 ...
- angularjs学习笔记三——directive
AngularJS 通过被称为 指令 的新属性来扩展 HTML. 正如你所看到的,AngularJS 指令是以 ng 作为前缀的 HTML 属性. HTML5 允许扩展的(自制的)属性,以 data- ...
- 转:Remote debugging with Visual Studio 2010
Original URL http://www.codeproject.com/Articles/146838/Remote-debugging-with-Visual-Studio-2010 you ...
- Swift互用性: 使用Objective-C特性编写Swift类(Swift 2.0版)-b
本节包括内容: 继承Objective-C的类(Inheriting from Objective-C Classes) 采用协议(Adopting Protocols) 编写构造器和析构器(Writ ...
- WordPress网站更换老鹰主机详细操作
眼看着之前买的虚拟主机就要到期了,本着节约至上的美德,就和同事一起买了老鹰主机.因为第一次网站的配置是一个朋友帮忙的,所以现在想完全自己动手操作,毕竟之后的博客维护还是得靠自己.下面就来和我一起学习怎 ...