HDU1711-----Number Sequence-----裸的KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711
题目意思:
找出b在a中的起始位置,没有则是-1
解题思路:
裸的KMP,不多说
不会KMP的话可以去看http://www.cppblog.com/oosky/archive/2006/07/06/9486.html
说的非常好
模板我是拿的大白的
代码:
#include<cstdio>
#include<cstring>
using namespace std; const int maxn = 10000+10;
const int maxn2 = 1000000+10; int a[maxn2];
int b[maxn]; int next[maxn]; void getnext(int T[],int len,int* qnext)
{
qnext[0] = 0;
qnext[1] = 0;
for(int i=1;i<len;i++)
{
int j = qnext[i];
while(j && T[i]!=T[j]) j = qnext[j];
qnext[i+1] = (T[i]==T[j])?j+1:0;
}
} int KMP(int S[],int T[],int len1,int len2)
{
getnext(T,len2,next);
int j=0;
for(int i=0;i<len1;i++)
{
while(j && S[i]!=T[j]) j = next[j];
if(T[j] == S[i])
j++;
if(j==len2)//已经找到匹配串
{
return i-len2+1;
} }
return -1;
} int main()
{
int t;
scanf("%d",&t);
int n,m;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);
int ans = KMP(a,b,n,m);
if(ans != -1)
printf("%d\n",ans+1);
else
printf("-1\n");
}
return 0;
}
HDU1711-----Number Sequence-----裸的KMP的更多相关文章
- [裸KMP][HDU1711][Number Sequence]
题意 找到子串在母串出现的第一个位置 解法 裸的KMP 特别的地方 第一次不看模板自己敲的KMP #include<stdio.h> const int maxn=100000; cons ...
- HDU1711 Number Sequence(KMP模板题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu1711 Number Sequence kmp应用
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目: Problem Description Given two sequences of n ...
- HDU1711 Number Sequence KMP
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU1711 题意概括 给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出 ...
- kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...
- hdu1711 Number Sequence kmp模板
题目传送门 学习博客 学习了kmp算法,理解了算法思想,但还没有到能把这个思想用语言来描述出来. #include<bits/stdc++.h> #define clr(a,b) mems ...
- HDU1711 Number Sequence 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:最基础的字符串匹配,只不过这里用整数数组代替了字符串. 给你两个数组 \(a[1..N ...
- hdu1711 Number Sequence
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- Number Sequence HDU 1711 KMP 模板
题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- APNs推送, 处理通知
设备接到apns发来的通知,应用处理通知有以下几种情况: 1. 应用还没有加载 这时如果点击通知的显示按钮,会调用didFinishLaunchingWithOptions,不会调用didReceiv ...
- Memcached(三)Memcached配置参数初解
一.基本参数在我们第一次安装Memcached时,一般都是用过这个命令: memcached -m 512 -u root -d -l 127.0.0.1 -p 11211 我们先来解释这几个参数的含 ...
- PHP练习题(二)
程序6.题目: 假设某人有100,000现金,每经过一次路口需要进行一次交费交费规则为当他现金大于50,000时每次需要交%5,如果现金小于等于50,000时每次交5,000.问此人可以经过多少次这个 ...
- 理解和熟练运用js中的call及apply
call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向. 因为 JavaScript 的函数存在「定义时上下文」和 ...
- 第 4 章 多例模式【Multition Pattern】
以下内容出自:24种设计模式介绍与6大设计原则 这种情况有没有?有!大点声,有没有? 有!,是,确实有,就出现在明朝,那三国期间的算不算,不算,各自称帝,各有各的地盘,国号不同.大家还记得那首诗< ...
- uva 11437 - Triangle Fun
计算几何: 直线交点: #include<cstdio> using namespace std; struct node { double x,y; node(,):x(x),y(y){ ...
- Linux搭建SVN 服务器
Linux搭建SVN 服务器 1 安装SVN 2 使用客户端连接 2.1 使用windows的客户端 2.2 使用Linux下的命令行 3 ...
- SharePoint 2010中使用Visual Studio 2010进行方便快速的Web Part开发
转:http://www.cnblogs.com/fatwhale/archive/2010/02/24/1672633.html 在Visual Studio 2010中, 已经集成了用于Shar ...
- Entity Framework系列文章导航
转自:http://www.cnblogs.com/xray2005/archive/2011/10/11/2206746.html Entity Framework4.0系列文章 需要说明的是,以下 ...
- (转)Ubuntu下彻底卸载mysql
感谢原作者,文章内容很实用.原文链接:http://www.blogjava.net/yjhmily/articles/336926.html ============================ ...