题目链接: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. Ubuntu搭建Android交叉编译环境

    一.下载 Android NDK Android NDK官方下载页:http://developer.android.com/tools/sdk/ndk/index.html如果需要旧版本的,比如10 ...

  2. Winform开发框架之简易工作流设计

    一讲到工作流,很多人第一反应就是这个东西很深奥,有时候又觉得离我们较为遥远,确实完善的工作流设计很多方面,而正是由于需要兼顾很多方面,一般通用的工作流都难做到尽善尽美.微软也提供了几个版本的WF框架支 ...

  3. 2015年最热门前端框架React 入门实例教程

    现在最热门的前端框架,毫无疑问是 React . 上周,基于 React 的 React Native 发布,结果一天之内,就获得了 5000 颗星,受瞩目程度可见一斑. React 起源于 Face ...

  4. ASP.NET MVC4 数据库连接(EF6.0)

    我的博客原文地址:http://www.star110.com/Note/ReadArticle/60641215331146140041.html 环境:.NET MVC4 + EF6.0 连接数据 ...

  5. Android入门:Activity四种启动模式

    一.启动模式介绍 启动模式简单地说就是Activity启动时的策略,在AndroidManifest.xml中的标签的android:launchMode属性设置: 启动模式有4种,分别为standa ...

  6. mybatis mapper association collection

    1.Question Description: sometimes, POJO bean contains another bean or collection as property, it's s ...

  7. C语言回滚(三)-指针

    #include <stdio.h>#include <stdlib.h> //& 地址运算符 //* 间接运算符 // *的作用 当*后面跟一个指针名或地址的时候, ...

  8. 那些教程没有的php4-composer依赖管理工具

    phpcomposer PHP 5.3.2+ Composer 不是一个包管理器,但它在每个项目的基础上进行管理,在你项目的某个目录中(例如 vendor)进行安装.默认情况下它不会在全局安装任何东西 ...

  9. centos如何安装软件

    背景 之前用的linux操作系统移植都是ubuntu,没有用过redhat版本的linux,最近开始想学习redhan版本的linux,就从centos开始.在安装完centos以后,第一个碰到的问题 ...

  10. asp xmlhttp 读取文件

    Response.Write LoadTxtFile("URL") Function LoadTxtFile(LoadFile) Dim XMLHTTP, XMLDOC, Resp ...