Number Sequence

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

Total Submission(s): 11691    Accepted Submission(s): 5336

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3336 3746 

pid=2203" target="_blank">2203 1710 3068 

题意:给两个数组a和b,求b在a中出现的的第一个位置,若没有则输出-1,仅仅是数组变成了整型数组,方法与字符数组全然一样。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1000005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std; int s[maxn],p[maxn],nextval[maxn];
int N,M; void get_nextval()
{
int i=0,j=-1;
nextval[0]=-1;
while (i<M)
{
if (j==-1||p[i]==p[j])
{
i++;
j++;
if (p[i]!=p[j])
nextval[i]=j;
else
nextval[i]=nextval[j];
}
else
j=nextval[j];
}
} int KMP()
{
int i=0,j=0;
while (i<N&&j<M)
{
if (j==-1||s[i]==p[j])
{
i++;
j++;
}
else
j=nextval[j];
}
if (j==M)
return i-M+1;
else
return -1;
} int main()
{
int cas;
scanf("%d",&cas);
while (cas--)
{
scanf("%d%d",&N,&M);
for (int i=0;i<N;i++)
scanf("%d",&s[i]);
for (int i=0;i<M;i++)
scanf("%d",&p[i]);
get_nextval();
int ans=KMP();
printf("%d\n",ans);
}
return 0;
}
/*
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
*/

hdu 1711 Number Sequence KMP 基础题的更多相关文章

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

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

  2. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  3. HDU 1711 Number Sequence (KMP 入门)

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

  4. HDU 1711 Number Sequence KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...

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

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

  6. HDU 1711 Number Sequence(数列)

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

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

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

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

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

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

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

随机推荐

  1. 域名做CDN来通过隐藏服务器真实IP的方法来防止DDoS攻击(转)

    隐藏服务器真实IP是解决问题最好和最快的方法,但只针对小流量,大流量同样会扛不住. 服务器前端加CDN中转,比如阿里云.百度云加速.360网站卫士.加速乐.安全宝等,如果资金充裕的话,可以购买高防的盾 ...

  2. Web安全测试指南--信息泄露

    5.4.1.源代码和注释: 编号 Web_InfoLeak_01 用例名称 源代码和注释检查测试 用例描述 在浏览器中检查目标系统返回的页面是否存在敏感信息. 严重级别 中 前置条件 1.  目标we ...

  3. Migrating Oracle on UNIX to SQL Server on Windows

    Appendices Published: April 27, 2005 On This Page Appendix A: SQL Server for Oracle Professionals Ap ...

  4. maven 创建web工程,spring配置文件找不到!

    公司项目用的maven,在启动服务器的时候,出现上面的错误,然后找到WEB-INF下面的web.xml,给classPath加上*号,解决!

  5. 性能问题: SQL*Net message from client 等待时间太长

    今天我终于自己遇到了这个问题, PO form 打不开了, 看了下 trace 发现 SQL*Net message from client 等待时间太长. 但是这不可能是网络问题, 这个环境是在我电 ...

  6. Go -- FileManage 自建云盘

    一.介绍 Caddy,用Go写的一款相当优秀的Web服务器软件,它有不少很有特色的功能,国内目前来说用的不多,不过也逐渐有越来越多的人知道了,它有个特色的插件功能,其中一款插件是FileManager ...

  7. 如何在Centos7上安装zookeeper 多实例

    一.如何在Centos7上安装zookeeper 多实例 cd /usr/local/src/ wget https://mirrors.tuna.tsinghua.edu.cn/apache/zoo ...

  8. lumisoft会将eml后缀格式的附件给解析成文本,这里保存成文件

    MIME_Entity[] attachments = mime.Attachments; foreach (MIME_Entity entity in attachments) { string f ...

  9. ISP图像调试工程师——tone Mapping(ISP)

    http://www.cnblogs.com/bigbigtree/p/3458797.html

  10. 纯 PHP 代码最好在文件末尾删除 PHP 结束标记

    如果文件内容是纯 PHP 代码,最好在文件末尾删除 PHP 结束标记.这可以避免在 PHP 结束标记之后万一意外加入了空格或者换行符,会导致 PHP 开始输出这些空白,而脚本中此时并无输出的意图. & ...