Number Sequence

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

Total Submission(s): 10571    Accepted Submission(s): 4814

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<iostream>

#include<cstdio>

using namespace std;

const int maxn1=1000010;

const int maxn2=10010;

int n,m;

int a[maxn1];

int b[maxn2];

int next[maxn2];

int main()

{

 int kmp (int *a,int *b,int *next);

 int t,i;

 cin>>t;

 while(t--)

 {

  cin>>n>>m;

  for(i=0;i<n;i++)

  {scanf("%d",&a[i]);}

  for(i=0;i<m;i++)

  {scanf("%d",&b[i]);}

  int ans=kmp(a,b,next);

  cout<<ans<<endl;

 }

 return 0;

}

//此函数用来求匹配串s串的next数组

void getnext (int *s,int *next)

{

    next[0]=next[1]=0;

    for (int i=1;i<m;i++)//m为匹配串s的长度

 {

        int j=next[i];

        while (j&&s[i]!=s[j])

            j=next[j];

        next[i+1]=s[i]==s[j]?

j+1:0;

    }

}

//此函数用来求匹配位置的值的函数。匹配不成功返回值-1

int kmp (int *a,int *b,int *next)

{

    getnext (b,next);/////////

    int j=0;

    for (int i=0;i<n;i++)

 {/////n为串1的长度

        while (j&&a[i]!=b[j])

            j=next[j];

        if (a[i]==b[j])

            j++;

        if (j==m)//m为串2的长度

            return i-m+2;

    }

    return -1;

}

HDU 1711 Number Sequence(字符串匹配)的更多相关文章

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

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

  2. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  3. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

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

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

  5. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

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

  6. hdu 1711 Number Sequence KMP 基础题

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

  7. HDU 1711 Number Sequence (KMP简单题)

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

  8. HDU 1711 Number Sequence (字符串处理 KMP)

    题目链接 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...

  9. KMP - HDU 1711 Number Sequence

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

随机推荐

  1. 启动Apache出现问题:一直停留在启动界面

    问题描述:  由于需要php_curl模块,因此直接在php.ini文件将前面的分号去掉  ,但是重启Apache时出现:一直停留在启动界面,Apache无法正常启动,查看错误日志,显示如下: 解决方 ...

  2. 图片格式 WebP APNG

    WebP  是一种支持有损压缩和无损压缩的图片文件格式,派生自图像编码格式 VP8.根据 Google 的测试,无损压缩后的 WebP 比 PNG 文件少了 45% 的文件大小,即使这些 PNG 文件 ...

  3. linux-less

    linux-less less是more命令的进化版.拥有与more命令一样的向前向后向下向上的数据查看功能,同时less还可以在查看内容中进行快速查找,关于数据向上向下操作,可以看看这个http:/ ...

  4. 配置好postfix邮件服务器之后就可以使用它来发送邮件了

    下面是一段摘自W3school关于php mail函数的栗子,经过测试发现两个问题. <?php $to = "somebody@example.com, somebodyelse@e ...

  5. Maven快速使用阿里云的代理maven仓库

    自从开源中国的maven仓库挂了之后就一直在用国外的仓库,慢得想要砸电脑的心都有了.如果你和我一样受够了国外maven仓库的龟速下载?快试试阿里云提供的maven仓库,从此不在浪费生命…… 仓库地址: ...

  6. 接受第三方app分享的数据

    前段时间公司项目需要一个需求: 把第三方的app分享的数据接受到自己的apk中, 涉及到的第三方app是: Youtube/Amazon/NetFlix, 这些app通过分享功能把当前的信息分享出去. ...

  7. Mac_Homebrew

    Homebrew作为OS X上强大的包管理器,为系统软件提供了非常方便的安装方式,独特式的解决了包的依赖问题,并不再需要烦人的sudo,一键式编译,无参数困扰,真的,你值得拥有. brew 的安装:  ...

  8. 使用sshkey的方式访问gitlab

    在使用jenkins创建jobs的时候配置git为ssh访问的方式报错 记录错误信息 Failed to connect to repository : Command "/usr/bin/ ...

  9. 转贴---Performance Counter(包含最全的Windows计数器解释)

    http://support.smartbear.com/viewarticle/55773/ 这个Article中介绍了一个新工具,TestComplete,把其中涉及到性能计数器的部分摘抄出来了. ...

  10. Kaggle实战之二分类问题

    0. 前言 1. MNIST 数据集 2. 二分类器 3. 效果评测 4. 多分类器与误差分析 5. Kaggle 实战 0. 前言 "尽管新技术新算法层出不穷,但是掌握好基础算法就能解决手 ...