ABC240Ex Sequence of Substrings
ABC240Ex Sequence of Substrings
LIS 的好题改编。
约定
\(S(l,r)\) 为字符串 \(s\) 中第 \(l\) 位到底 \(r\) 位。
\(S(l,r)<S(x,y)\) 为字符串中 \([l,r]\) 的子串字典序比 \([x,y]\) 的子串小。
前置
LIS 的 \(n\log n\) 求法。
题解
我们考虑按照类似于朴素 LIS 的方式设状态,\(f[l][r]\) 表示 \([l,r]\) 这个区间作为当前选的最后一个划分,所得到的 LIS 最大值。
显然有转移:
\]
要求 \(S(x,y)<S(l,r)\)。
我们可以使用一个很经典的判断两个字符串字典序大小的技巧,先使用 hash+二分 求出 LCP(最长公共前缀),然后用比较 LCP 的下一位求字典序大小。
对于方程里的 \(\max\) 操作,类似于 LIS 的 \(n\log n\) 做法维护一个 \(g\) 数组,之前 \(g[i]\) 表示 LIS 为 \(i\) 的最小数字,同时 \(g\) 数组有单调递增的性质。现在还是维护这样的一个 \(g\) 数组,\(g[i]\) 表示 LIS 为 \(i\) 的字典序最小区间,\(g[i]\) 可以用一个 pair 类型维护。
当然为了方便,笔者把数组变成了 set,维护相同的东西,方便直接使用 lower_bound 查询。
每一个 \(f[l][r]\) 都要做一次上述转移,转移复杂度包含:\(g\) 数组查找的 \(O(\log n)\),每次的查找的比较 \(O(\log n)\),共 \(O(\log^2 n)\)。总共复杂度 \(O(n^2\log^2 n)\)。
这个复杂度是肯定过不了的,我们考虑从这题的性质上去优化。
每一个 \(S(l,r)\) 肯定是从一个比他小的串 \(S(x,y)\) 转移过来的,我们可以分两种情况讨论:
- \(S(l,r)\) 靠长度比 \(S(x,y)\) 大。
- \(S(l,r)\) 通过字符比较比 \(S(x,y)\) 大。
考虑通过 2 类型的方式转移,那么 \(S(l,r)\) 的长度肯定小于等于 \(S(x,y)\)。
考虑通过 1 类型做贡献的子串的最大长度是 \(B\)。显然 \(B\) 肯定是从 \(1\) 开始累加起来的,那么前面肯定出现过长度为 \(B-1,B-2,B-3,\cdots,1\) 通过 1 类型转移的子串,他们总共的长度为 \(\frac{(1+B)\times B}{2}\),满足
\]
解得
\]
说明了什么呢?
通过 1 类型做贡献的子串最大长度是 \(\sqrt{2n}\),通过 2 类型做贡献的子串长度小于等于最大子串长度。
那么我们每次只需要求子串长度在 \(\sqrt{2n}\) 以内状态,即只需要求满足 \(r-l+1\leq \sqrt{2n}\) 所有 \(f[l][r]\)。
时间复杂度降至 \(O(n\sqrt n \log^2 n)\)。
擦一把汗还是可以过的,信友队高级组 T1 和这题重了,实测也可以跑过。
#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
const int maxn=2e4+5e3+5;
#define ll long long
#define pii pair<int,int>
#define S second
#define F first
int n,B;
char s[maxn];
int ans;
ll sum[maxn],base[maxn];
unordered_map<int,int>f[maxn];
inline ll gt(int r,int l)
{
return (sum[r]-sum[l-1]*base[r-l+1]%mod+mod)%mod;
}
inline bool cmps(int x,int rx,int y,int ry)
{
return gt(rx,x)==gt(ry,y);
}
inline bool cmp(int x,int rx,int y,int ry)
{
int l=1,r=min(rx-x+1,ry-y+1),ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(cmps(x,x+mid-1,y,y+mid-1)) l=mid+1,ans=mid;
else r=mid-1;
}
if(ans==min(rx-x+1,ry-y+1)) return rx-x+1<ry-y+1;
return s[x+ans]<s[y+ans];
}
struct node
{
int l,r,w;
bool operator<(const node a)const{return cmp(l,r,a.l,a.r);}
};
set<node>st;
pii fd[maxn];
int main()
{
scanf("%d",&n);
B=sqrt(2*n);
base[0]=1;for(int i=1;i<=n;i++) base[i]=base[i-1]*113%mod;
scanf("%s",s+1);
for(int i=1;i<=n;i++) sum[i]=(sum[i-1]*113+s[i]-'0'+mod)%mod;
for(int L=1;L<=n;L++)
{
for(int R=L;R<=n&&R<=L+B;R++)
{
auto it=st.lower_bound({L,R,0});
if(it!=st.begin()) f[L][R]=(--it)->w+1;
else f[L][R]=1;
}
for(int j=L;j;j--)
{
if(L-j+1>B) break;
if(fd[f[j][L]].F==0)
{
st.insert({j,L,f[j][L]});
fd[f[j][L]]={j,L};
}
else if(cmp(j,L,fd[f[j][L]].F,fd[f[j][L]].S))
{
st.erase({fd[f[j][L]].F,fd[f[j][L]].S,f[j][L]});
st.insert({j,L,f[j][L]});fd[f[j][L]]={j,L};
}
}
}
printf("%d",st.size());
}
ABC240Ex Sequence of Substrings的更多相关文章
- A. Robot Sequence
A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp
C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- Codeforces 626A Robot Sequence
A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 626A Robot Sequence(模拟)
A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- 8VC Venture Cup 2016 - Elimination Round A. Robot Sequence 暴力
A. Robot Sequence 题目连接: http://www.codeforces.com/contest/626/problem/A Description Calvin the robot ...
- Codeforces 626 A. Robot Sequence (8VC Venture Cup 2016-Elimination Round)
A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence
Easy Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- HDOJ 5357 Easy Sequence DP
a[i] 表示以i字符开头的合法序列有多少个 b[i] 表示以i字符结尾的合法序列有多少个 up表示上一层的'('的相应位置 mt[i] i匹配的相应位置 c[i] 包括i字符的合法序列个数 c[i ...
- CodeForces 5C Longest Regular Backet sequence
This is yet another problem dealing with regular bracket sequences. We should remind you that a brac ...
随机推荐
- C++11新特性(一):语言特性
C++11新特性 总结C++11特性时发现整个内容角度,建议查看前先查看目录. 语言特性 右值引用 右值的分类为将亡值和字面量.将亡值就是将要销毁的对象以及临时的变量,字面量就是常量.左值就是变量. ...
- 【Mac + Appium + Java1.8(二)】之Android模拟器自动化测试脚本开发以及简易例子
直接上代码: import io.appium.java_client.AppiumDriver; import org.junit.After; import org.junit.Before; i ...
- 一个小小空格问题引起的bug
程序员会遇到一种情况,一个bug排查到最后是由一个很小的问题导致的.在昨天的日常搬砖中遇到一个问题,耽搁了我大半天的时间,最后查明原因让我很无语. 首先介绍一下背景,我是做算法模型训练,目前手上的工作 ...
- git重命名文件夹
在源代码文件夹中打开git bash, 不同名称的文件夹命令: 1. git mv A An 3. git add -u An 4. git commit -m "重命名A为An&quo ...
- [Udemy] AWS Certified Data Analytics Specialty - 6.Security
S3 加密 SSE-S3 SSE-KMS SSE-C Client Side Encryption SSL/TLS S3 支持http/https 两种协议 KMS KMS最大能加密4KB的数据,再大 ...
- 编译和分发 Chez Scheme 应用程序
参考 Building and Distributing Applications. 假设源码由两个文件组成,A.ss 和 B.ss,其中 A.ss 依赖 B.ss.下面我们将其编译为可供分发的二进制 ...
- 痞子衡嵌入式:在MDK开发环境下自定义安装与切换不同编译器版本的方法
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是在MDK开发环境下自定义安装与切换不同编译器版本的方法. Keil MDK 想必是嵌入式开发者最熟悉的工具之一了,自 2005 年 Ar ...
- `->` 操作符重载的注意事项
在 C++ 中,-> 操作符可以被重载,用于对象的指针成员访问. 重载后的 -> 操作符主要用于模拟指针访问行为,常见于智能指针的实现等复杂场景. -> 操作符重载后的调用触发流程: ...
- MySQL9的3个新特性
本文讲解MySQL9的3个新特性:支持将JSON输出保存到用户变量.支持准备语句以及支持面向AI的向量存储. 17.12 MySQL9新特性1--支持将JSON输出保存到用户变量 从MySQL 9版 ...
- balance_dirty_pages_ratelimited分析
balance_dirty_pages_ratelimited分析 nr_dirtied_pause:当前task的脏页门限: dirty_exceeded:全局的脏页数超过门限或者该bdi的脏页数超 ...