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 ...
随机推荐
- linq的一些用法总结
获取列表数据. IList<Model> list = dao.getmx(Model, pageInfo);//获取数据列表 1.将列表中id一样的数据进行group by分组,并返回序 ...
- sql问题
表中某个指标重复,去掉重复项: select * from #temp where A0107 in (select A0107 from #temp group by A0107having CO ...
- [!] CocoaPods was not able to update the `master` repo...
输入pod install之后出现: [!] CocoaPods was not able to update the `master` repo. If this is an unexpected ...
- Spring依赖注入的三种方式
看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...
- Vim简明教程【CoolShell】(转)
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- N3292x IBR介绍
N3292x IBR介绍 1 IBR启动流程 图1-1 IBR启动流程 CHIP_CFG[0] Mode 0 Boot from IBR Recovery Mode with crystal inpu ...
- tomcat启动报错:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable
linux 下 启动tomcat 报: Neither the JAVA_HOME nor the JRE_HOME environment variable is definedAt least o ...
- 在Linux-0.11中实现基于内核栈切换的进程切换
原有的基于TSS的任务切换的不足 进程切换的六段论 1 中断进入内核 2 找到当前进程的PCB和新进程的PCB 3 完成PCB的切换 4 根据PCB完成内核栈的切换 5 切换运行资源LDT 6 利用I ...
- Linq学习系列
LINQ之路系列博客导航 http://www.cnblogs.com/lifepoem/archive/2011/12/16/2288017.html LINQ体验系列文章导航 http://www ...
- 富文本web编辑器(UEditor)
展示效果: