Number Sequence

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 90   Accepted Submission(s) : 57

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

HDU 2007-Spring Programming Contest 
 #include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
int a[];
int b[];
int nxt[];
int n, m;
int kmp()
{
int i, j;
j = ;
for (i = ; i <= n; i++)
{
while (j&&b[j + ] != a[i]) j = nxt[j];
if (a[i] == b[j+])
{
j++;
}
if (j == m)
{
return i - m + ;
}
}
return -;
}
int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n >> m;
int i, j;
for (i = ; i <= n; i++)
{
cin >> a[i];
}
for (i = ; i <= m; i++)
{
cin >> b[i];
}
j = ;
for (i = ; i <= m; i++)
{
while (j&&b[j + ] != b[i]) j = nxt[j];
if (b[j + ] == b[i]) j++;
nxt[i] = j;
}
int f = kmp();
cout << f << endl;
}
return ;
}

#include <iostream>#include <algorithm>#include <cstring>#include <string>using namespace std;int a[1000005];int b[10005];int nxt[10005];int n, m;int kmp(){int i, j;j = 0;for (i = 1; i <= n; i++){while (j&&b[j + 1] != a[i]) j = nxt[j];if (a[i] == b[j+1]){j++;}if (j == m){return i - m + 1;}}return -1;}int main(){int t;cin >> t;while (t--){cin >> n >> m;int i, j;for (i = 1; i <= n; i++){cin >> a[i];}for (i = 1; i <= m; i++){cin >> b[i];}j = 0;for (i = 2; i <= m; i++){while (j&&b[j + 1] != b[i]) j = nxt[j];if (b[j + 1] == b[i]) j++;nxt[i] = j;}int f = kmp();cout << f << endl;}return 0;}

1711 Number Sequence(kmp)的更多相关文章

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

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

  2. HDU 1711 Number Sequence(kmp)

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

  3. HDU 1711 Number Sequence (KMP)

    白书说这个是MP,没有对f 数组优化过,所以说KMP有点不准确 #include <stdio.h> int a,b; int T[1000010],P[10010];//从0开始存 in ...

  4. HDU 1711 Number Sequence(数列)

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

  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. Number Sequence(kmp)

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

  8. hdu 1711 Number Sequence(kmp找子串第一次出现的位置)

    题意:裸kmp 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string.h> using nam ...

  9. HDU 1711 Number Sequence(KMP模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...

随机推荐

  1. class文件的结构

    任何一个class文件都对应着唯一一个类或接口的定义信息,但反过来说,类或接口的定义信息并不一定都在文件里(比如类或接口也可以通过类加载器直接生成) class文件是一组以8位字节为基础单位的二进制流 ...

  2. POJ 2699 The Maximum Number of Strong Kings (最大流+枚举)

    http://poj.org/problem?id=2699 题意: 一场联赛可以表示成一个完全图,点表示参赛选手,任意两点u, v之间有且仅有一条有向边(u, v)或( v, u),表示u打败v或v ...

  3. UVa 1658 海军上将(最小费用最大流)

    https://vjudge.net/problem/UVA-1658 题意: 给出一个v个点e条边的有向加权图,求1~v的两条不相交(除了起点和终点外公共点)的路径,使得权和最小. 思路:把2到v- ...

  4. 一年25个里程碑!免疫疗法“战胜”癌症,靠的是实力(5篇Science、6篇Nature )--转载

    近几年,免疫疗法的成功使癌症治疗进入了新的时代.无论是科研界,还是商业界,都丝毫没有掩饰对这一领域的热情.2016年,Cell杂志公布的年度十大最佳论文中,免疫疗法占两席.事实上,这两项成果只是去年癌 ...

  5. bzoj1084: [SCOI2005]最大子矩阵 dp

    这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. 题解:m很小分类讨论,m==1时怎么搞都可以,m==2时,dp[i][j][k]表 ...

  6. 通读cheerio API-网络爬虫

    所谓工欲善其事,必先利其器,所以通读了cheerio的API,顺便翻译了一遍,有些地方因为知道的比较少,不知道什么意思,保留了英文,希望各位不吝告诉我,然后一起把这个翻译完成. ###cheerio ...

  7. CF 160D Edges in MST 最小生成树的性质,寻桥,缩点,批量处理 难度:3

    http://codeforces.com/problemset/problem/160/D 这道题要求哪条边存在于某个最小生成树中,哪条边不存在于最小生成树中,哪条边绝对存在于最小生成树中 明显桥边 ...

  8. hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0

    H - National Day Parade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  9. elasticsearch 自定义similarity 插件开发

    转自:http://www.chepoo.com/elasticsearch-similarity-custom-plug-in-development.html 在搜索开发中,我们要修改打分机制,就 ...

  10. js设计模式中发布与订阅实现观察者模式例子

    <script> var pubsub = {}; (function(q) { var topics = {}; subuid = -1; q.publish = function(to ...