题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711

问题描述:给两个序列a,b,长度分别为n,m(1<=n<=1000000,1<=m<=10000),问序列b是否为序列a的子序列,若:返回a中最左边的与b相等的子序列的首元素下标;若不是,输出-1。

目的:方便以后查看KMP算法中next[]的模板

Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12811    Accepted Submission(s): 5815

Problem Description
  Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.
 
Input
  The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].
 
Output
  For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
 
Sample Input
2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1
 
Sample Output
6
-1

代码实现:

 #include "stdio.h"
#include "string.h"
#define N 10005 int next[N];
int a[*N],b[N]; void KMP(int *s,int len,int *next) //求next[]模板
{
int i,j;
i = ;
j = next[] = -;
while(i<len)
{
while(j!=- && s[i]!=s[j])
j = next[j];
next[++i] = ++j;
}
} int main()
{
int T;
int i,j;
int n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
for(i=; i<n; i++) scanf("%d",&a[i]);
for(i=; i<m; i++) scanf("%d",&b[i]);
KMP(b,m,next);
i=j=;
while(i<n)
{
while(j!=- && a[i]!=b[j])
j = next[j];
i++,j++;
if(j==m) break;
}
if(j==m) printf("%d\n",i-j+); //题中数据是从下标为1开始的,故加1
else printf("-1\n");
}
return ;
}

字符串_KMP算法(求next[]模板 hdu 1711)的更多相关文章

  1. 【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 [DFS染色] #include<iostream> #include<cstdio&g ...

  2. Prime算法 与 Kruskal算法求最小生成树模板

    算法原理参考链接 ==> UESTC算法讲堂——最小生成树 关于两种算法的复杂度分析 ==> http://blog.csdn.net/haskei/article/details/531 ...

  3. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  4. HDU 1269 迷宫城堡 tarjan算法求强连通分量

    基础模板题,应用tarjan算法求有向图的强连通分量,tarjan在此处的实现方法为:使用栈储存已经访问过的点,当访问的点离开dfs的时候,判断这个点的low值是否等于它的出生日期dfn值,如果相等, ...

  5. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  6. HDU - 1711 A - Number Sequence(kmp

    HDU - 1711 A - Number Sequence   Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...

  7. STL算法与树结构模板

    STL算法 STL 算法是一些模板函数,提供了相当多的有用算法和操作,从简单如for_each(遍历)到复杂如stable_sort(稳定排序),头文件是:#include <algorithm ...

  8. HDU 1711(KMP)字符串匹配

    链接  HDU 1711 Number Sequence KMP 算法 我以自己理解写的,写的不对,不明白的地方海王子出来,一起共同学习: 字符串匹配 就是KMP,一般思想,用一个for循环找开头   ...

  9. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

随机推荐

  1. 准备.Net转前端开发-WPF界面框架那些事,值得珍藏的8个问题

    题外话 不出意外,本片内容应该是最后一篇关于.Net技术的博客,做.Net的伙伴们忽喷忽喷..Net挺好的,微软最近在跨平台方面搞的水深火热,更新也比较频繁,而且博客园的很多大牛也写的有跨平台相关技术 ...

  2. CSS布局 -- 左右定宽,中间自适应

    左右定宽,中间自适应 有几种方法可以实现 改变窗口大小看看? 方案一: 左右设置绝对定位,定宽,中间设置margin-left  margin-right 查看 demo <!DOCTYPE h ...

  3. P6 EPPM R16.1安装与配置指南(一)

    标题 http://www.cnblogs.com/endv/p/5634620.html 安装与配置指南安装与配置指南(数据库)说明哪些How to set up the P6专业数据库和服务器.a ...

  4. 重构第5天:提升字段(Pull Up Field)

    理解:提升字段和前面讲解的方法提公很类似,可以说方式都是一样的.就是把继承类中经常用到的字段,提出来 放到基类中,达到通用的目的.提高代码重用性和可维护性. 详解:如下重构前的代码: using Sy ...

  5. ADO.NET完整增删改

    完整增添 删除 修改 运用中间变量运用.bool has=false 1增加 需要使用cmd.Parameters这个集合 占位符: @key 代表这个位置用这个占位符占住了 SqlConnectio ...

  6. sql跨数据库转移

    结构一样的话insert into 数据库A.dbo.TableAselect * from 数据库B.dbo.TableA 另外:nsert into DDD(字段1,字段2,字段3 .....)( ...

  7. mysql备份方法

    在用PHP或者其他web脚本语言构架的应用系统中,数据库大部分是mysql其中就牵扯到了一个数据库日常备份导出等问题,大概有以下几种情况: 1.如果应用用的是开源的产品,如dz.pw和其他cms等一般 ...

  8. 【Asphyre引擎】今天终于把精灵demo基本改好了。

    doudou源代码 包含Sprite代码(Sprite还没改完,粒子特效有些问题,但是基本上可以用了) Stage1-1.map  不好意思,漏了地图配置.

  9. Wechat4j之Hello world——使用wechat4j快速开发java版微信公众号

    Wechat4j是一个开源的java微信开发框架,是目前最简单易用的java微信开发框架. 项目地址:https://github.com/sword-org/wechat4j Wechat4j.ja ...

  10. jekyll

    bundle show minima查看安装路径 bundle exec github-pages versions 建立一个类似于master的分支,与master是完全独立 git checkou ...