http://acm.hdu.edu.cn/showproblem.php?pid=1711

Number Sequence

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1358 3336 1686 3746 1251
 
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[] ;
int b[]; void getnext(int *b , int len , int *next)//next是记录字符串的每个字符的之前的字符串的最长的前缀与后缀
{
next[] = -;//将next数组右移一项使与查找时下标匹配
int j = , k = - ;
while(j < len - )
{
if(k == - || b[k] == b[j])
{
k++;
j++;
next[j] = k ;
}
else
{
k = next[k];
}
}
} int main()
{
int t ;
cin >> t ;
while(t--)
{
int next[];
int n , m ;
scanf("%d%d" , &n ,&m);
for(int i = ; i < n ; i++)
{
scanf("%d" , &a[i]);
}
for(int i = ; i < m ; i++)
{
scanf("%d" , &b[i]);
}
getnext(b , m , next);//求next数组
int i = , j = ;
while(i < n && j < m)
{
if(j == - || a[i] == b[j])
{
j++ ;
i++ ;
}
else
{
j = next[j];//文本i不动,b串移动到最长前缀与后缀的长度下标
}
}
if(j == m)
printf("%d\n" , i - j + );
else
printf("-1\n"); } return ;
}

kmp(单次匹配)的更多相关文章

  1. KMP算法,匹配字符串模板(返回下标)

    //KMP算法,匹配字符串模板 void getNext(int[] next, String t) { int n = next.length; for (int i = 1, j = 0; i & ...

  2. KMP入门(匹配)

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

  3. FZU 2122 又见LKity(KMP+返回所有匹配位置)

    基础kmp应用,找到所有匹配位置即可 #include<stdio.h> #include<string.h> #include<algorithm> #inclu ...

  4. jQuery 获取对象 根据属性、内容匹配, 还有表单元素匹配

    指定元素中包含 id 属性的, 如: $("span[id]") 代码如下: <span id="span1" name="S1"&g ...

  5. jQuery 基础 : 获取对象 根据属性、内容匹配, 还有表单元素匹配

    指定元素中包含 id 属性的, 如: $("span[id]")   <span id="span1" name="S1">AA ...

  6. Oulipo POJ - 3461(kmp,求重叠匹配个数)

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  7. KMP算法——字符匹配

     暴力匹配: 假设现在我们面临这样一个问题:有一个文本串S,和一个模式串P,现在要查找P在S中的位置,怎么查找呢? 如果用暴力匹配的思路,并假设现在文本串S匹配到 i 位置,模式串P匹配到 j 位置, ...

  8. KMP算法-字符匹配

    字符匹配模式-KMP算法 j直接跳到了2的位置,因为在之前的都相同. 那么就需要求如果不等了之后,j需要回跳的位置next[j] 如果tk'与tj相等,则next [j+1]=k'+1 如果tk'与t ...

  9. hdu 2087 剪花布条 KMP多次匹配

    剪花布条 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?   I ...

随机推荐

  1. maven整合S2SH

    1.pom.xml <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.ap ...

  2. [好好学习]在VMware中安装Oracle Enterprise Linux (v5.7) - (1/5)

    在想到Oracle Enterprise Linux,第一时间就是在Oracle官网上找到资源,但是,出现以下图片

  3. jvm监控和诊断工具

    大牛写的Java的OOM Killer:https://www.jianshu.com/p/4645254be259 强烈推荐 总的参考链接:https://cloud.tencent.com/dev ...

  4. nginx-博客阅读笔记记录-20190916

    Nginx 入门学习教程 Ng官网解释: nginx [engine x]是最初由Igor Sysoev编写的HTTP和反向代理服务器,邮件代理服务器和通用TCP / UDP代理服务器. 维基百科解释 ...

  5. alert(1) to win 9

    function escape(s) { function htmlEscape(s) { return s.replace(/./g, function(x) { return { '<': ...

  6. 前端自动化gulp使用方法

    gulp介绍 1. 网站: http://slides.com/contra/gulp#/ 2. 特点 易于使用:通过代码优于配置的策略, Gulp 让简单的任务简单,复杂的任务可管理. 构建快速 : ...

  7. Nginx-配置负载均衡实例

    配置负载均衡实例 实现效果: 配置负载均衡 实验代码 1) 首先准 备两个同时启动的 Tomcat 2) 在 nginx.conf 中进行配置 随着互联网信息的爆炸性增长,负载均衡(load bala ...

  8. centos6.5安装nginx1.16.0

    参考:   centos7 编译安装nginx1.16.0( 完整版 ) https://blog.csdn.net/weixin_37773766/article/details/80290939  ...

  9. 4,fail-fast错误机制

    一,fail-fast简介 在JDK的Collection中我们时常会看到类似于这样的话: ArrayList 注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出 ...

  10. php中间件是什么

    php中间件(middleware)是一个闭包,而且返回一个闭包. 中间件为过滤进入应用的HTTP请求提供了一套便利的机制,可以分为前置中间件和后置中间件.常用于验证用户是否经过认证,添加响应头(跨域 ...