Number Sequence

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

Total Submission(s): 10239    Accepted Submission(s): 4656

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

经典 kmp。

AC代码例如以下:

#include<iostream>
#include<cstdio>
using namespace std;
int a[1000005],b[10005],next[10005];
int main()
{
int t;
int n,m,ans;
int i,j;
scanf("%d",&t);
while(t--)
{
ans=-1;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
i=0;j=-1;next[0]=-1;
while(i<m)
{
if(j==-1||b[i]==b[j])
next[++i]=++j;
else
j=next[j];
}
i=0;j=0;
while(i<n&&j<m)
{
if(j==-1||a[i]==b[j])
i++,j++;
else j=next[j];
if(j==m)
{
ans=i-j+1;break;
}
}
printf("%d\n",ans);
}
return 0;
}

杭电 1711 Number Sequence的更多相关文章

  1. HDU 1711 Number Sequence(数列)

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

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

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

  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. Rabin_Karp(hash) HDOJ 1711 Number Sequence

    题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...

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

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

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

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

  7. HDOJ 1711 Number Sequence

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

  8. KMP - HDU 1711 Number Sequence

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

  9. hdoj 1711 Number Sequence【求字串在母串中第一次出现的位置】

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

随机推荐

  1. reStructuredText(.rst)语法规则快速入门

    原文:http://blog.useasp.net/archive/2014/09/05/rst-file-restructuredtext-markup-syntax-quikstart.aspx? ...

  2. 基于FPGA的DW8051移植(三)

    总结一下问题: 1) http://www.cnblogs.com/sepeng/p/4137405.html  基于FPGA的DW8051移植(一)里面用modelsim观测波形发现程序进入了ida ...

  3. 多层TreeWidget可选实现

    http://download.csdn.net/detail/lingyunfuyu2/9117481

  4. 鼠标滑轮一滚动Excel就停止工作

    鼠标滑轮一滚动Excel就停止工作 问题签名: 问题事件名称:APPCRASH 应用程序名:EXCEL.EXE 应用程序版本:15.0.4420.1017 应用程序时间戳:50673286 故障模块名 ...

  5. 深入解析MFC -- 句柄与对象的关系

    CWnd::FromHandlePermanent ——根据窗口句柄得到CWnd*指针 This function, unlike FromHandle, does not create tempor ...

  6. 完整的yuicompressor单个压缩和批量压缩以及gzip再次压缩,拦截器的配置等

    下载地址:http://yuilibrary.com/download/yuicompressor/ 个人认为现在yuicompressor是最安全,最值得信赖的压缩工具,至少到现在没出现过问题 1. ...

  7. hdu4704之费马小定理+整数快速幂

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  8. 已知的CPropertysheet bug: 切换焦点导致无响应

    当一个页面内容比较多时我们首先可能考虑用Tab Control,但如果有很多页面内容需要动态加载则用CPropertySheet比较好点~ CPropertySheet有两种不同的显示模式.一种就是向 ...

  9. iTextSharp

    iTextSharp 116毫秒处理6G的文件   前言: 有一家印刷企业专为米兰新娘,微微新娘,金夫人这样的影楼印刷婚纱相册.通过一个B2B销售终端软件,把影楼的相片上传到印刷公司的服务器,服务器对 ...

  10. MySQL主键添加/删除

    2改动数据库和表的字符集alter database maildb default character set utf8;//改动数据库的字符集alter table mailtable defaul ...