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. Checkbox in DataList

    一,效果图. 二,源代码. <!DOCTYPE html><html><head> <meta charset="UTF-8"> & ...

  2. XCode 6 出现 no identity found: Command /usr/bin/codesign failed with exit code 1 解决方法汇总

    1, 解决办法,进入开发者账号重建一个 Provisioning Profiles(或配套证书) 文件,把证书添加正确就可以了 (应该是最有效的) 2, 将p12文件重新安装下 3, 在 iPhone ...

  3. PyQt中如何隐藏Menu

    PyQt中隐藏一个Menu Item,可以通过QAction的setVisible(False)来设置,而QMenu的setVisible(False)是不管用的. 现在问题来了,我们有一个菜单,它有 ...

  4. POJ 1142 质因数分解

    只要很朴素的分解就可以了,数据量不大 #include <stdio.h> #include <string.h> #include <stdlib.h> #inc ...

  5. POJ1054 枚举【STL__binary_search()_的应用】

    ①使用binary_search前要先保证有序 ②binary_search函数仅返回true或false ③binary_search(first element, laste lment + 1, ...

  6. 为什么要用BASE64

    BASE64和其他相似的编码算法通常用于转换二进制数据为文本数据,其目的是为了简化存储或传输.更具体地说,BASE64算法主要用于转换二进 制数据为ASCII字符串格式.Java语言提供了一个非常好的 ...

  7. perl 使用use utf8

    jrhapt12:/home/tomcat> cat a1.pl use Encode; $phone='18072722237'; open (LOG1 ,"<",' ...

  8. 自定义安装Apache+php+mysql网站服务器环境

    自定义安装Apache+php+mysql 这种方式是比较麻烦的安装方式,需要具有一定的对Apache了解的基础上才能安装,安装顺序就是先安装Apache软件,然后安装php,最后安装mysql.这里 ...

  9. Mars的mp3实例

    Mars的mp3实例第一课: 关于menu: package mars.mp3player01; import mars.down.HttpDownloader; import android.app ...

  10. ZOJ 3603字符串操作

    解题思路:找到公共子串然后升序输出 坑的地方就在于输入是存在相同字母的 #include <stdio.h> #include <algorithm> #include < ...