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 ...
随机推荐
- OSX10.11 CocoaPods 升级总结
本文不会讨论CocoaPods的各种使用技巧以及各种原理,只是简单记录一下在升级过程中遇到的问题,如果使用中有各种问题来欢迎交流. Podfile.loc 文件变化 前几天一个小伙更新了CocoaPo ...
- yii 载入css or js
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/TableView.js&q ...
- Bernese安装及使用
一.安装: 伯尔尼软件的安装很简单,但是在64位下,可能perl解释器安装不成功,我找了一个,并且可用,下载地址: 链接:http://pan.baidu.com/s/1hr8fgEC 密码:fj8b ...
- Sniffer抓包教程
上网络信息安全的时候用了下,中途出现了一堆奇葩的事,这里就不提了... 上教程: 先把虚拟机里面的防火墙给关了,主机防火墙也关了 之前由于ip自己设置了,然后一直ping不通,后面把ip改成自动获取就 ...
- 后台地址报错:Service Unavailable
首先考虑数据库是否打开? 第二重启IIS试试: 重启下iis试试(cmd接着iisreset) 再次访问就正常了,可以借鉴,但不一定就只是这一种原因.
- MySQL无法登录服务器解决方法
提示:#2000 无法登录 MySQL 服务器 今天用本机装了个phpMyAdmin,版本3.4.8,想用它来连一台内网服务器上的Mysql,于是乎修改phpMyAdmin配置文件config.inc ...
- [转] 与调试器共舞 - LLDB 的华尔兹
你是否曾经苦恼于理解你的代码,而去尝试打印一个变量的值? NSLog(@"%@", whatIsInsideThisThing); 或者跳过一个函数调用来简化程序的行为? NSNu ...
- 挂载NTFS
1.安装ntfs-3g wget https://tuxera.com/opensource/ntfs-3g_ntfsprogs-2015.3.14.tgz --no-check-certificat ...
- Spring MVC PageNotFound.noHandlerFound No mapping found for HTTP request with URI
首先骂人,干他娘的,弄了两个小时原来的包倒错了!!唉TMD. 注意用IDEA倒包的时候一定要注意ModelAndView是 原因是import出错了!!应该是import org.springfram ...
- Java JNDI Datasource HOW-TO Problem
在开发JAVA的时候发生了点问题,解决方案记录一下,在这里http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto. ...