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. IAsyncResult接口

    #region 程序集 mscorlib.dll, v4.0.0.0 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framewor ...

  2. 解决 "OperationalError: (sqlite3.OperationalError) no such table: ..."问题

    参考:flask/sqlalchemy - OperationalError: (sqlite3.OperationalError) no such table 在用Flask写一个简单的py文件,做 ...

  3. zeptojs库解读1之整体框架

    首先看的是整体框架, // zepto骨骼,这个函数的作用使得Zepto(slector, context)使用很多$.fn里面的方法 var Zepto = (function(){ // zept ...

  4. poj 2828 Buy Tickets 树状数组

    Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in China, so ...

  5. 使用git bush 生成github SSH公钥

    1 如果没有安装ssh,那么使用下面的指令 sudo apt-get install ssh 2 检查SSH公钥 cd ~/.ssh 看看存不存在.ssh,如果存在的话,掠过下一步:不存在的请看下一步 ...

  6. Android Studio 中实现高德定位并获取相应信息

    Android开发项目时常常会遇到定位这个功能,所以写了这篇博客,今天主要讲的高德地图的定位并获取相应信息. 首先导入高德的jar包 选中jar包右键点击  Add As Library, 在buil ...

  7. 第108天:Ajax中XMLHttpRequest详解

    在Ajax应用程序中,XmlHttpRequest对象负责将用户信息以异步通信地发送到服务器端,并接收服务器返回的响应信息和数据. XMLHttpRequest可以提供不重新加载页面的情况下更新网页, ...

  8. python运行错误------Non-UTF-8 code

    1.安装-----见:https://www.cnblogs.com/weven/p/7252917.html 本文转载于:http://blog.csdn.net/youyuyixiu/articl ...

  9. 处理EXCEL11问题

    程序原本是好使的,但是自从卸载OFFICE11而安装14后,程序无法启动. 重新安装2003 ,然后,删除原引用 Microsoft.Office.Interop.Excel,然后添加引用,浏览,找到 ...

  10. Ubuntu 16.04 日常工具

    shutter sudo apt-get install shutter indicator-sysmonitor 之前需要通过deb包安装,现在可以通过添加PPA安装: sudo add-apt-r ...