Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24928    Accepted Submission(s):
10551

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
 
 
思路:
  kmp模板;
 
 
来,上代码:

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std; const int N = ;
int nest[],n,m;
int s[],t[];
int slen,tlen; void getnest()
{
memset(nest,,sizeof(nest));
int j,k;
j=;k=-;nest[]=-;
while(j<tlen)
{
if(k==-||t[j]==t[k])
nest[++j]=++k;
else k=nest[k];
}
} int kmp_index()
{
int i=;int j=;
getnest();
while(i<slen&&j<tlen)
{
if(j==-||s[i]==t[j])
{
i++;j++;
}
else j=nest[j];
}
if(j==tlen) return i-tlen+;
else return -;
} int kmp_count()
{
int ans=;
int i,j=;
if(slen==&&tlen==)
{
if(s[]==t[]) return ;
else return ;
}
getnest();
for(i=;i<slen;i++)
{
while(j>&&s[i]!=t[j])
{
j=nest[j];
}
if(s[i]==t[j]) j++;
if(j==tlen)
{
ans++;
j=nest[j];
}
}
return ans;
} int main()
{
int TT;
int i,cc;
cin>>TT;
while(TT--)
{
cin>>n>>m;
for(int i=;i<n;i++)
cin>>s[i];
for(int j=;j<m;j++)
cin>>t[j];
tlen=m,slen=n;
cout<<kmp_index()<<endl;
}
return ;
}

AC日记——Number Sequence hdu 1711的更多相关文章

  1. (KMP 模板)Number Sequence -- Hdu -- 1711

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

  2. Number Sequence HDU 1711(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...

  3. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

  4. Number Sequence HDU 1711 KMP 模板

    题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...

  5. Number Sequence ----HDOJ 1711

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

  6. Number Sequence(HDU 1005 构造矩阵 )

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. Number Sequence HDU - 5014

    There is a special number sequence which has n+1 integers. For each number in sequence, we have two ...

  8. AC日记——Keywords Search hdu 2222

    2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...

  9. AC日记——Super Mario hdu 4417

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. (66)zabbix导入/导出配置文件

    通过导入/导出zabbix配置文件,我们可以将自己写好的模板等配置在网络上分享,我们也可以导入网络上分享的配置文件 配置文件有两种格式,分为为xml与json,通过zabbix管理界面可以导出xml, ...

  2. Linux中的常见命令

    1.   ls    查看当前目录下的所有文件夹 2.   pwd  查看当前所在的文件夹 3. cd 目录名 切换文件夹 4. touch 文件名 创建文件 5. mkdir 目录名 创建文件夹 6 ...

  3. ASP( VBScript ) 解析 JSON

    <script language="jscript" runat="server"> Array.prototype.get = function( ...

  4. 14-15.Yii2.0模型的创建/读取数据使用,框架防止sql注入

    目录 创建数据库 表article 配置 db.php 连接数据库 创建控制器 HomeController.php 创建models 创建数据库 表article 1.创建库表 CREATE TAB ...

  5. 【结构型模式】《大话设计模式》——读后感 (12)在NBA我需要翻译?——适配器模式

    适配器模式:将一个类的接口转换成客户希望的另外一个接口,Adapter模式使得原本由于接口不兼容而不能在一起工作的 那些类可以在一起工作了[DP] UML类图: 简单模拟一下代码: //已存在的.具有 ...

  6. 采用Atlas+Keepalived实现MySQL读写分离、读负载均衡

    ========================================================================================== 一.基础介绍 == ...

  7. Python9-网络编程-day30

    # 由于不同机器上的程序要通信,才产生了网络# server# client# 端口 找到的程序# 在计算机上,每一个需要网络通信的程序,都会开一个端口# 在同一时间只会有一个程序占用一个端口# 不可 ...

  8. LeetCode(215) Kth Largest Element in an Array

    题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...

  9. Shuffle UVA - 12174 尺取法

    题目:题目链接 思路:见紫书,对具体操作方式还不是很理解,代码是从一个题解里看的,以后多回顾下,需要理解 代码: #include <iostream> #include <cstr ...

  10. 文件上传下载,命令之wget / curl / which / sort / uniq / cut / wc /tr /sed

    目录 命令 1.文件的上传下载 2.从外网下载文件wget 3.curl文件下载 4.查找命令which 5.字符处理命令-排序sort 6.字符处理-去重uniq 7.字符处理-截取cut 8.字符 ...