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 最大值。

显然有转移:

\[f[l][r]=\max(f[x][y])+1
\]

要求 \(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)\) 转移过来的,我们可以分两种情况讨论:

  1. \(S(l,r)\) 靠长度比 \(S(x,y)\) 大。
  2. \(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}\),满足

\[\frac{(1+B)\times B}{2} \leq n
\]

解得

\[B \leq \sqrt{2n}
\]

说明了什么呢?

通过 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的更多相关文章

  1. A. Robot Sequence

    A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. 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 ...

  3. Codeforces 626A Robot Sequence

    A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. Codeforces 626A Robot Sequence(模拟)

    A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  5. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  6. 8VC Venture Cup 2016 - Elimination Round A. Robot Sequence 暴力

    A. Robot Sequence 题目连接: http://www.codeforces.com/contest/626/problem/A Description Calvin the robot ...

  7. 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 ...

  8. 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 ...

  9. HDOJ 5357 Easy Sequence DP

    a[i] 表示以i字符开头的合法序列有多少个 b[i] 表示以i字符结尾的合法序列有多少个 up表示上一层的'('的相应位置 mt[i] i匹配的相应位置 c[i] 包括i字符的合法序列个数  c[i ...

  10. CodeForces 5C Longest Regular Backet sequence

    This is yet another problem dealing with regular bracket sequences. We should remind you that a brac ...

随机推荐

  1. 去除WinForm程序中的Devexpress弹窗

    去除WinForm程序中的Devexpress弹窗 /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static ...

  2. 【Git】之分支合并命令

    一.Git分支管理 1.查看Git分支 git branch 按Q键返回. 2.创建分支以及删除分支 git branch newtest git branch -d newtest 3.切换分支 g ...

  3. 【YashanDB知识库】主备延迟故障分析方法

    [标题]主备延迟故障分析方法 [问题分类]故障分析 [关键字]Yashandb.主备延迟 [问题描述]当数据库备机出现回放延迟时,需要通过一些手段分析延迟的原因.通过数据库的系统视图或操作系统监控数据 ...

  4. 9.5内网横向&代理&隧道

    Socks代理思路: 工具:nps.frp.ngrok.reGeorg.sockscap64.earthworm.proxifier.proxychains 知识点 1.内外网简单知识 2.内网1和内 ...

  5. 八,SpringBoot Web 开发访问静态资源(附+详细源码剖析)

    八,SpringBoot Web 开发访问静态资源(附+详细源码剖析) @ 目录 八,SpringBoot Web 开发访问静态资源(附+详细源码剖析) 1. 基本介绍 2. 快速入门 2.1 准备工 ...

  6. Typora mac激活

    typora mac版本激活 我也是第一次使用mac电脑,在安装时基本上都是付费的,在mac下载使用typora是试用一段时间后是需要付费购买的,苦无能力有限只能绕一下,感谢网上的各位大佬的分享 来源 ...

  7. Webpack4-使用expose-loader将变量注册到全局

    首先贴一下 package.json中的插件版本,不同的版本也许会有很大的差异: // package.json{ "name": "WEBPACK-DEV-1" ...

  8. element plus 2.3.14(完成 指南 部分)

    https://element-plus.org/zh-CN/guide/design.html 设计 控制反馈: 通过界面样式和交互动效让用户可以清晰的感知自己的操作: 页面反馈: 操作后,通过页面 ...

  9. JDBC——案例

    创建一个商品表 drop table if exists tb_brand; -- 创建tb_brand表 create table tb_brand( id int primary key auto ...

  10. element 的 表单 搜索 和 重置按钮换行问题 ? inline

    想要: 但是: 加上