http://acm.hdu.edu.cn/showproblem.php?pid=1711

Number Sequence

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

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
 
Source

Next里面存的是前缀和后缀的最大相似度

Next[i] 代表的是前 i 个数的最大匹配

#include<stdio.h>
#include<string.h>
#include<stdlib.h> #define N 1000007 int M[N], S[N], Next[N]; void FindNext(int Slen)
{
int i=, j=-;
Next[] = -; while(i<Slen)
{
if(j==- || S[i]==S[j])
Next[++i] = ++j;
else
j = Next[j];
}
} int KMP(int Mlen, int Slen)
{
int i=, j=; FindNext(Slen); while(i<Mlen)
{
while(j==- || (M[i]==S[j] && i<Mlen && j<Slen))
i++, j++;
if(j==Slen)
return i-Slen+; j = Next[j];
}
return -;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, i; scanf("%d%d", &n, &m); for(i=; i<n; i++)
scanf("%d", &M[i]);
for(i=; i<m; i++)
scanf("%d", &S[i]); printf("%d\n", KMP(n, m));
}
return ;
}

(KMP 模板)Number Sequence -- Hdu -- 1711的更多相关文章

  1. AC日记——Number Sequence hdu 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

  3. Number Sequence HDU 1711 KMP 模板

    题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...

  4. Number Sequence HDU 1711(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...

  5. Number Sequence ----HDOJ 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. Number Sequence HDU - 5014

    There is a special number sequence which has n+1 integers. For each number in sequence, we have two ...

  7. Number Sequence(HDU 1005 构造矩阵 )

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. KMP匹配算法 - Number Sequence

    Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...

  9. 【KMP】Number Sequence

    KMP算法 KMP的基处题目,数字数组的KMP算法应用. 主要是next[]数组的构造,next[]存储的是字符的当前字串,与子串前字符匹配的字符数. 移动位数 = 已匹配的字符数 - 对应的部分匹配 ...

随机推荐

  1. virtual abstract override new 几点学习

    1.Vitual方法和普通方法区别为:继承其的子类可以用override/new在重载此方法,也可以不重载其方法,有方法体(可以写语句),override修饰则调用子类方法2.abstract类中抽象 ...

  2. Moment-JavaScript 日期处理类库

    来源:http://momentjs.cn/ 日期格式化 moment().format('MMMM Do YYYY, h:mm:ss a'); // 二月 22日 2017, 4:04:26 下午 ...

  3. MySQL语句相关

    一.增加 1.基本 2.集合 3.组合 二.删除 1.基本 2.集合 3.组合 1.一个表的查询结果作为另一个表的插入字段之一 <insert id="方法" paramet ...

  4. vs2010下载Microsoft Visual Studio 2010 Express(vs2010中文版下载)速成官方合集正式版

    http://www.xiazaiba.com/html/1832.html VB.NET 2010 Express: 2KQT8-HV27P-GTTV9-2WBVV-M7X96VC++ 2010 E ...

  5. codeblocks c++11 pthread

    支持c++11: setting->compiler-> 打上勾即可.(如果没有c++11,那么请更新codeblocks最新版.) pthread_create错误: 由于pthread ...

  6. Maven项目标准目录结构

    -----------------------siwuxie095 Maven 项目标准目录结构 1.Maven 项目分为两种 (1)Java 项目 (2)Web 项目 2.对于 Java 项目 其中 ...

  7. python之socket运用之传输大文件

    socket建议最大的传输单元是8192个字符,但是如果超过8192就会出现问题,我们可以用下面的方法处理 客户端代码 import subprocess import socket ip_bind ...

  8. [leetcode]199. Binary Tree Right Side View二叉树右视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  9. 用递归方法求 n!

    #include <iostream> using namespace std; #define LL long long LL fac(int n) { LL f; || n == ) ...

  10. [SoapUI] Datasink怎么显示每次循环的结果

    https://www.soapui.org/reporting/the-report-datasink.html