kmp(单次匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=1711
Number Sequence
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.
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].
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
-1
#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(单次匹配)的更多相关文章
- KMP算法,匹配字符串模板(返回下标)
//KMP算法,匹配字符串模板 void getNext(int[] next, String t) { int n = next.length; for (int i = 1, j = 0; i & ...
- KMP入门(匹配)
Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M ...
- FZU 2122 又见LKity(KMP+返回所有匹配位置)
基础kmp应用,找到所有匹配位置即可 #include<stdio.h> #include<string.h> #include<algorithm> #inclu ...
- jQuery 获取对象 根据属性、内容匹配, 还有表单元素匹配
指定元素中包含 id 属性的, 如: $("span[id]") 代码如下: <span id="span1" name="S1"&g ...
- jQuery 基础 : 获取对象 根据属性、内容匹配, 还有表单元素匹配
指定元素中包含 id 属性的, 如: $("span[id]") <span id="span1" name="S1">AA ...
- Oulipo POJ - 3461(kmp,求重叠匹配个数)
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- KMP算法——字符匹配
暴力匹配: 假设现在我们面临这样一个问题:有一个文本串S,和一个模式串P,现在要查找P在S中的位置,怎么查找呢? 如果用暴力匹配的思路,并假设现在文本串S匹配到 i 位置,模式串P匹配到 j 位置, ...
- KMP算法-字符匹配
字符匹配模式-KMP算法 j直接跳到了2的位置,因为在之前的都相同. 那么就需要求如果不等了之后,j需要回跳的位置next[j] 如果tk'与tj相等,则next [j+1]=k'+1 如果tk'与t ...
- hdu 2087 剪花布条 KMP多次匹配
剪花布条 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? I ...
随机推荐
- iOS开发笔记1
1.在堆上模拟函数调用栈 背景: 在看算法书时候, 很多地方提到要谨防递归的栈溢出问题. 分析: 递归调用时候, 有可能出现非常深的函数调用. 对于每次的函数调用, 都需要将函数体内的局部变量保存在栈 ...
- 同步请求与异步请求Json
同步请求的返回值类型 : void : 啥也不返回 String :表示逻辑视图名 ModelAndView:该对象既有逻辑视图名,还可以携带去页面要展示的数据 同步请求:如何将controller层 ...
- python基础--3 列表
#list类#li是list类的一个对象li=[11,22,33,22,44] #参数#在原来值最后进行整个作为元素追加 # li.append((11,22,33))#对列表本身进行操作,appen ...
- 怎样group by一列 select多列
之前sql用的少 竟然不知道这个小技巧 1 将要查询的列 添加到group by后面(会影响查询结果) 2 使用聚合函数如 max select a.accounttitlecode, max(b.c ...
- 二、MyBatis-HelloWorld
环境准备 1.创建数据库表 create table tbl_employee ( id ) primary key AUTO_INCREMENT comment "ID", la ...
- SpringBoot---事务支持
1.自动配置的事务管理器 1.1.使用JDBC 作为 数据访问技术 时,SpringBoot 为我们 定义了 PlatformTransactionManager的实现 DataSourc ...
- centos7标准版命令界面和图形界面相互切换
1.root登陆终端 2.输入命令 vi /etc/inittab ,查看两种界面的启动模式: 3.退出vi模式,,输入命令systemctl get-default 查看当前系统启动模式:我的是命令 ...
- AGC040
AGC040 A 模拟. B 因为顺序无关紧要,所以可以先把区间按右端点排序方便处理. 设第一个区间在\(A\)集合,考虑枚举第一个在\(B\)集合的区间\(i\),这样两个集合的右端点\(\min\ ...
- Appium解决native+webview混合型APP(公众号、小程序)切换webview后元素无法定位问题
问题:最近在做一个安卓+H5混合开发的APP自动化测试,发现在从native切换到webview后,元素仍然无法找到,报错:no such element 思路:于是思考webview会不会像web页 ...
- [CSP-S模拟测试]:石头剪刀布(rps)(概率DP)
题目传送门(内部题9) 输入格式 第一行一个整数$n$.接下来$n$行每行$3$个非负整数$r_i,p_i,s_i$. 输出格式 一行一个实数表示答案.当你的答案与标准答案的绝对或相对误差不超过${1 ...