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. matlplotlib绘图(二)

    matplotlib基础知识 matpltlib中的基本图表包括的元素 1.x轴和y轴:水平和垂直的轴线 2.x轴和y轴的刻度:刻度标识坐标值的分隔,包括最小刻度和最大刻度 3.x轴和y轴刻度:表示特 ...

  2. 常用的windows小工具指令和如何打开自定义的程序

    windows可以通过 开始->运行->输入程序名 或 windows键+R键 两种方式来启动windows中自带的程序或手动安装的程序.下面介绍一些常用的windows工具的指令和如何打 ...

  3. jQuery plugin : bgStretcher 背景图片切换效果插件

    转自:http://blog.dvxj.com/pandola/jQuery_bgStretcher.html bgStretcher 2011 (Background Stretcher)是一个jQ ...

  4. OOP中常用到的函数

    学习地址: http://www.jikexueyuan.com/course/2420.html 判断类是否存在 class_exists() 得到类或者对象中的成员方法组成的数组 get_clas ...

  5. OpenCV中图像的读取,显示与保存

      图像的读取,显示与保存 相关函数:cv2.imread().cv2.imshow().cv2.imwrite() 1.读入图像: 用cv2.imread()函数来读取图像,cv2.imread(路 ...

  6. Java-basic-4-数据类型

    Number类 装箱:将内置数据类型作为包装类对象使用:拆箱:相反 public class test{ public static void main(String args[]) { // box ...

  7. LeetCode(282) Peeking Iterator

    题目 Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peek ...

  8. 牛客练习赛22 C 简单瞎搞题

    //位运算 // & 都是1 才是 1 // | 都是0 才是0 // ^ 不一样才是1 #include <iostream> #include <cstdio> # ...

  9. Leetcode 81. 搜索旋转排序数组 II

    题目链接 https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/description/ 题目描述 假设按照升序排序的数 ...

  10. Linux磁盘分区介绍

    分区?我们不是已经在BIOS界面分区好了吗?如果领导给你一块磁盘,你怎么用呢?所以就有了分区工具(fdisk和parted),fdisk工具只针对小于2T磁盘分区,且是交互式的:parted很强大,通 ...