Number Sequence

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

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

题意:

给出T和P求p出现的位置

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
const int maxm=;
int p[maxm],t[maxn];
int f[maxm],n,m;
void getfail(int* p,int* f){
f[]=;f[]=;
for(int i=;i<m;i++){
int j=f[i];
while(j&&p[i]!=p[j]) j=f[j];
f[i+]=(p[i]==p[j]?j+:);
}
}
int find(int* t,int* p,int* f){
getfail(p,f);
int j=;
for(int i=;i<n;i++){
while(j&&p[j]!=t[i]) j=f[j];
if(p[j]==t[i]) j++;
if(j==m) return i-m++;//下标从0开始
}
return -;
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++) scanf("%d",&t[i]);
for(int i=;i<m;i++) scanf("%d",&p[i]);
int ans=find(t,p,f);
printf("%d\n",ans);
}
return ;
}

HDU1711 KMP(模板题)的更多相关文章

  1. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  2. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  3. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

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

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

  5. hdu 1711 Number Sequence(KMP模板题)

    我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...

  6. HDU 1711Number Sequence【KMP模板题】

    <题目链接> 题目大意: 意思是给出两个串,找出匹配串在模式串中的位置. 解题分析: KMP算法模板题. #include <cstdio> #include <cstr ...

  7. POJ:3461-Oulipo(KMP模板题)

    原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...

  8. [HDU1711]KMP模板

    解题关键:1.直接套kmp模板即可,注意最后输出的位置,需要在索引的位置+1. 2.next用作数组名在oj中会编译错误, 3.选用g++,只有g++才会接受bits/stdc++.h OJ中g++和 ...

  9. (模板)poj3461(kmp模板题)

    题目链接:https://vjudge.net/problem/POJ-3461 题意:给出主串和模式串,求出模式串在主串中出现的次数. 思路:kmp板子题. AC代码: #include<cs ...

  10. HDU1711 Number Sequence(KMP模板题)

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

随机推荐

  1. cronolog:日志分割工具

    一. 引言 因为tomcat的catalina.out日志无法按照日期自动创建,因此采用cronnlog分割. 二. 安装与配置 1.安装cronolog: yum install -y cronol ...

  2. Android开发-API指南-<supports-screens>

    <supports-screens> 英文原文:http://developer.android.com/guide/topics/manifest/supports-screens-el ...

  3. 【Linux 运维】查看网络连接状态信息之netstat和ss命令详解

    一.netstat 常用命令详解 通过man netstat可以查看netstat的帮助信息: netstat 命令:用于显示各种网络相关信息,如网络连接,路由表,接口状态,无效连接,组播成员 等等. ...

  4. Centos7添加静态路由

    本文摘取自 Centos7系统配置上的变化(二)网络管理基础 一.ip route显示和设定路由 1.显示路由表 [root@centos7 ~]# ip route show default via ...

  5. 浪在ACM新春大作战

    题目链接: # Name 补题状态 A Memory and Crow 已补 B Memory and Trident 已补 C Memory and De-Evolution 已补 D Memory ...

  6. Paper Reading - CNN+CNN: Convolutional Decoders for Image Captioning

    Link of the Paper: https://arxiv.org/abs/1805.09019 Innovations: The authors propose a CNN + CNN fra ...

  7. POJ 3845 Fractal(计算几何の旋转缩放)

    Description Fractals are really cool mathematical objects. They have a lot of interesting properties ...

  8. Bacon's Cipher(培根密码)

    Description Bacon's cipher or the Baconian cipher is a method of steganography (a method of hiding a ...

  9. Cstring, TCHAR*, char*的转换

    最近老用到Cstring, TCHAR*, char*的转换. 找到一篇写得蛮详细的. 引用过来, 方便自己以后查阅. char是类型TCHAR也是!不过他可以通过是否定义了UNICODE宏来判断到底 ...

  10. 【OpenGL】无法启动此程序,因为计算机中丢失 glut32.dll。尝试重新安装该程序以解决此问题。

    运行OpenGL程序的时候报错,如图: 解决方法:把glut32.dll复制到C:\Windows\SysWOW64目录下,而不是像网上教程那样复制到C:\Windows\System32目录下. 原 ...