BZOJ2384:[CEOI2014]Match
浅谈\(KMP\):https://www.cnblogs.com/AKMer/p/10438148.html
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=2384
这是一种特殊的\(KMP\)。匹配不再是直接判断相等了。
假设现在\([1,j]\)和\([i-j,i-1]\)已经匹配上了,我要判断\(j+1\)与\(i\)是否能继续扩展。
其实很简单,我们只需要判断在\([1,j]\)里面\(j+1\)这一位的前驱和后继与\(j+1\)的相对位置是否在\([i-j,i-1]\)里面与\(i\)也满足同样的关系即可。
前驱和后继的相对位置可以利用链表倒着扫一遍求出来。
时间复杂度:\(O(n)\)
空间复杂度:\(O(n)\)
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e6+5;
int n,m,ans[maxn];
int pos[maxn],pre[maxn],nxt[maxn];
int a[maxn],b[maxn],s1[maxn],s2[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
bool check(int *x,int j,int i) {
return (!a[j]||x[i-a[j]]<x[i])&&(!b[j]||x[i-b[j]]>x[i]);
}
int main() {
n=read(),m=read();
for(int i=1;i<=n;i++) {
s1[pos[i]=read()]=i;
pre[i]=i-1,nxt[i]=i+1;
}
for(int i=1;i<=m;i++)s2[i]=read();
for(int i=n;i;i--) {
a[i]=pre[s1[i]]?i-pos[pre[s1[i]]]:0;
b[i]=nxt[s1[i]]!=n+1?i-pos[nxt[s1[i]]]:0;
if(pre[s1[i]])nxt[pre[s1[i]]]=nxt[s1[i]];
if(nxt[s1[i]]!=n+1)pre[nxt[s1[i]]]=pre[s1[i]];
}
memset(nxt,0,sizeof(nxt));
for(int i=2,j=0;i<=n;i++) {
while(j&&(!check(s1,j+1,i)))j=nxt[j];
if(check(s1,j+1,i))j++;nxt[i]=j;
}
for(int i=1,j=0;i<=m;i++) {
while(j&&(!check(s2,j+1,i)))j=nxt[j];
if(check(s2,j+1,i))j++;
if(j==n) {ans[++ans[0]]=i-n+1,j=nxt[j];}
}
printf("%d\n",ans[0]);
for(int i=1;i<=ans[0];i++)
printf("%d ",ans[i]);
return 0;
}
BZOJ2384:[CEOI2014]Match的更多相关文章
- 【BZOJ2384】[Ceoi2011]Match KMP
[BZOJ2384][Ceoi2011]Match Description 作为新一轮广告大战的一部分,格丁尼亚的一家大公司准备在城市的某处设置公司的标志(logo).公司经理决定用一些整栋的建筑来构 ...
- 一款免费好用的正则表达式工具:Regex Match Tracer
推荐分享:一款免费好用的正则表达式工具:Regex Match Tracer v2.1.5 free version 下载地址:Regex Match Tracer
- 检查正则表达式的工具:Regex Match Tracer
Regex Match Tracer破解版下载 使用: 相关: 收藏几个好用的在线正则验证网
- HDU3605:Marriage Match IV
Marriage Match IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- linux设备驱动归纳总结(八):2.match.probe.remove
linux设备驱动归纳总结(八):2.总线.设备和驱动的关系 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- HDU 3081:Marriage Match II(二分图匹配+并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意:有n个男生n个女生,他们只有没有争吵或者女生a与男生A没有争吵,且女生b与女生a是朋友,因此女生b也 ...
- [bzoj1892][bzoj2384][bzoj1461][Ceoi2011]Match/字符串的匹配_KMP_树状数组
2384: [Ceoi2011]Match 1892: Match 1461: 字符串的匹配 题目大意: 数据范围: 题解: 很巧妙的一道题呀. 需要对$KMP$算法有很深的理解才行. 首先我们需要发 ...
- C#反射调用 异常信息:Ambiguous match found.
异常信息(异常类型:System.Reflection.AmbiguousMatchException)异常提示:Ambiguous match found.异常信息:Ambiguous match ...
- 【bzoj2384】[Ceoi2011]Match 特殊匹配条件的KMP+树状数组
题目描述 给出两个长度分别为n.m的序列A.B,求出B的所有长度为n的连续子序列(子串),满足:序列中第i小的数在序列的Ai位置. 输入 第一行包含两个整数n, m (2≤n≤m≤1000000). ...
随机推荐
- 【转】ftrace 简介
ftrace 简介 ftrace 的作用是帮助开发人员了解 Linux 内核的运行时行为,以便进行故障调试或性能分析. 最早 ftrace 是一个 function tracer,仅能够记录内核的函数 ...
- nginx和php-fpm的启停和配置
一.nginx的启停 (1) 启动nginx /etc/init.d/nginx start (2) 停止nginx /etc/init.d/nginx stop (3) 重启nginx /etc/i ...
- 【Python】装饰器理解
以下文章转载自:点这里 关于装饰器相关的帖子记录在这里: 廖雪峰, thy专栏, stackflow Python的函数是对象 简单的例子: def shout(word="yes" ...
- 微信授权网页获取用户openiid
微信网页授权:官方文档: https://mp.weixin.qq.com/wiki 支付文档:https://pay.weixin.qq.com/wiki/doc/api/index.html 调试 ...
- C++11_ Variadic Templates
版权声明:本文为博主原创文章,未经博主允许不得转载. 这次主要介绍C++11的又一个新特性 Variadic Templates (可变模板参数) 它的实现类似于initializer_list< ...
- vue.js 源代码学习笔记 ----- instance event
/* @flow */ import { updateListeners } from '../vdom/helpers/index' import { toArray, tip, hyphenate ...
- 编写configure.ac
configure.ac由一些宏组成(如果已经有源代码,你可以运行autoscan来产生一个configure.scan文件,在此基础修改成configure.ac将更加方便) 最基本的组成可以是下面 ...
- Miscosoft Visual Studio项目guid取值
There isn't an easy way to change the type of a project in Visual Studio project once it is created; ...
- MySQL笔记之多表
貌似很久不写博客了,趁着项目刚完结,记录一下这段时间了解到的一些认识,或许有些地方理解的还不够到位,看到的希望能不吝赐教. 外键 概念 与索引的关系 写法 查询 一对多多对一查询 多对多查询 插入 多 ...
- Redis的集群安装以及rehash重新迁移教程指南
1. Redis的cluster集群 在官方文档Cluster Spec中,作者详细介绍了Redis集群为什么要设计成现在的样子.最核心的目标有三个: 性能:这是Redis赖以生存的看家本领,增加集群 ...