题解-CodeForces700E Cool Slogans
Problem
题目大意:给定一个字符串,每次取出出现至少两次的子串替换原串,问最多能替换多少次,输出答案加一(字符串长为\(2×10^5\))
Solution
前置技能:SAM、线段树合并、dp
首先可以想到一个dp(设t为在s中出现至少两次的子串):\(dp[s]=\max\{dp[t] \}+1\)
然后想到如果t在s中出现不止一次,则在考虑t时,要求在s的endpos集合中一定存在一个处于区间\(\bigl[t_{longest}+s_{pos}-s_{longest},s_{longest}\bigr )\)的endpos,至于求endpos可以使用线段树合并求解
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rg register
const int N=401000,M=8001000;
int stp[N],ch[N][26],pre[N],pos[N],top[N],rt[N];
int ls[M],rs[M],b[N],f[N],tpy[N];
int n,tot=1,cnt,Ans=1,lst=1;
char s[N];
inline void ins(int x){
int p=lst,np=++tot;
lst=tot;stp[np]=stp[p]+1;
while(p&&!ch[p][x])ch[p][x]=np,p=pre[p];
if(!p)pre[np]=1;
else {
int q=ch[p][x];
if(stp[q]==stp[p]+1)pre[np]=q;
else {
int nq=++tot;stp[nq]=stp[p]+1;pos[nq]=pos[q];
for(rg int i=0;i<26;++i)ch[nq][i]=ch[q][i];
pre[nq]=pre[q];pre[q]=pre[np]=nq;
while(ch[p][x]==q)ch[p][x]=nq,p=pre[p];
}
}return ;
}
inline int merge(int u,int v){
if(!u||!v)return u+v;
int z=++cnt;
ls[z]=merge(ls[u],ls[v]);
rs[z]=merge(rs[u],rs[v]);
return z;
}
inline void update(int l,int r,int&x,int al){
if(!x)x=++cnt;
if(l==r)return ;
int mid(l+r>>1);
if(al<=mid)update(l,mid,ls[x],al);
else update(mid+1,r,rs[x],al);
return ;
}
inline int query(int l,int r,int x,int L,int R){
if(!x)return 0;
if(L<=l&&r<=R)return 1;
int mid(l+r>>1),res(0);
if(L<=mid)if(query(l,mid,ls[x],L,R))return 1;
if(mid<R)if(query(mid+1,r,rs[x],L,R))return 1;
return 0;
}
int main(){
scanf("%d%s",&n,s+1);
for(rg int i=1;i<=n;++i){ins(s[i]-'a');update(1,n,rt[lst],i);pos[lst]=i;}
for(rg int i=1;i<=tot;++i)++b[stp[i]];
for(rg int i=1;i<=n;++i)b[i]+=b[i-1];
for(rg int i=tot;i;--i)tpy[b[stp[i]]--]=i;
for(rg int i=tot;i^1;--i)rt[pre[tpy[i]]]=merge(rt[pre[tpy[i]]],rt[tpy[i]]);
for(rg int i=2;i<=tot;++i){
int x(tpy[i]),fa(pre[x]);
if(fa==1){f[x]=1,top[x]=x;continue;}
if(query(1,n,rt[top[fa]],pos[x]-(stp[x]-stp[top[fa]]),pos[x]-1))f[x]=f[fa]+1,top[x]=x;
else f[x]=f[fa],top[x]=top[fa];
Ans=max(Ans,f[x]);
}printf("%d\n",Ans);return 0;
}
题解-CodeForces700E Cool Slogans的更多相关文章
- CodeForces700E Cool Slogans
感谢dalaoWJZ的讲解. 我们对于每一个串a[i]相当于在他parent的right集合里找一个出现位置在id-len[x]+len[parent]到id[x]-1区间的 用主席树判存在性即可. ...
- 【CF700E】Cool Slogans 后缀自动机+线段树合并
[CF700E]Cool Slogans 题意:给你一个字符串S,求一个最长的字符串序列$s_1,s_2,...,s_k$,满足$\forall s_i$是S的子串,且$s_i$在$s_{i-1}$里 ...
- 【CF700E】Cool Slogans(后缀自动机)
[CF700E]Cool Slogans(后缀自动机) 题面 洛谷 CodeForces 题解 构建后缀自动机,求出后缀树 现在有个比较明显的\(dp\) 设\(f[i]\)表示从上而下到达当前点能够 ...
- [LOJ 6288]猫咪[CF 700E]Cool Slogans
[LOJ 6288]猫咪[CF 700E]Cool Slogans 题意 给定一个字符串 \(T\), 求一个最大的 \(K\) 使得存在 \(S_1,S_2,\dots,S_k\) 满足 \(S_1 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
随机推荐
- JVM jinfo命令(Java Configuration Info) 用法小结
简介 jinfo是jdk自带的命令,可以用来查看正在运行的Java应用程序的扩展参数,甚至支持在运行时,修改部分参数. 通常会先使用jps查看java进程的id,然后使用jinfo查看指定pid的jv ...
- VirtualBox下安装Ubuntu Server 16.04
安装环境: Windows:确保磁盘空间足够,一般需要8个G左右. 所需文件: 首先在Ubuntu的官网上下载.iso的镜像文件,链接是:http://www.ubuntu.org.cn/server ...
- Github/github 初始化教程
注: 由于将项目迁移到gitee,克隆gitee 的时候出现了问题.不得已,重新配置 ref : https://blog.csdn.net/jingtingfengguo/article/detai ...
- WebStorm记录(1)
开始写前端,使用WebStorm,记录下使用过程 参考 WebStorm 初步使用 & HTML5 学习报告 webstorm怎么运行调试html WebStorm 快速开发教程 --CSS篇 ...
- 使用JMeter进行一次简单的带json数据的post请求测试,json可配置参数
配置: 1.新建一个线程组: 然后设置线程数.运行时间.重复次数. 2.新建Http请求: 设置服务器域名,路径,方法,编码格式,数据内容. 可以在函数助手中,编辑所需要的变量,比如本例中的随机生成电 ...
- GBK 字符集
什么是 GBK ? 中文名 汉字编码字符集 外文名 Chinese Internal Code Specification 全 称 <汉字内码扩展规范> GBK编码,是对GB2312 ...
- hadoop的基本概念 伪分布式hadoop集群的安装 hdfs mapreduce的演示
hadoop 解决问题: 海量数据存储(HDFS) 海量数据的分析(MapReduce) 资源管理调度(YARN)
- Log4j2 快速开始
1.配置 默认 Log4j2可以将自己配置为记录错误及更高级别日志,并将消息记录到控制台中. [显示配置]1.检测log4j.configurationFile系统属性,如果属性存在,就从指定文件加载 ...
- JQuery之Attr()与Prop()方法
一.Prop()的由来 JQuery 1.6开始 新增方法prop() prop()解决:表单元素中checked,selected,disabled等属性在方法attr()中可能会出现的不一致问题( ...
- linux scanf函数%d后加空白
参考链接: https://bbs.csdn.net/topics/390389059 关键点: scanf()中空白字符(包括/n,space)会使scanf()函数在读操作中略去输入中的零个或者一 ...