题解-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 只做出来四道比较水的题目,还需要加强中等题的训练 ...
随机推荐
- MyBatis-${}与#{}
一.看两种取值的效果 <select id="selectMyUserIdAndAge" resultType="myUser"> select * ...
- springboot下整合redis使用redisTemplate模板
pom <!-- 引入 redis 依赖 --> <dependency> <groupId>org.springframework.boot</groupI ...
- Expressions versus statements in JavaScript
Statements and expressions An expression produces a value and can be written wherever a value is exp ...
- BZOJ 4318 OSU!(概率DP)
题意 osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: 一共有n次操作,每次操作只有成功与失败之分,成功对应1,失败对应0,n次操作对应为1个长度为n的01串.在 ...
- Golang入门教程(三)beego 框架安装
beego 是一个快速开发 Go 应用的 HTTP 框架,他可以用来快速开发 API.Web 及后端服务等各种应用,是一个 RESTful 的框架,主要设计灵感来源于 tornado.sinatra ...
- oldboys21day03
# 1.有变量name = "aleX leNb " 完成如下操作:'''name = "aleX leNb "'''# 移除 name 变量对应的值两边的空格 ...
- 调用waitpid的SIGCHLD信号处理函数
#include <stdio.h> #include <sys/wait.h> void sig_chld(int signo) { pid_t pid; int stat; ...
- git 完全讲解 无废话,包含在myeclipse中使用,包括解决冲突
Git 1. Git简介 1.1 git是什么 1.1.1概念 Git:git是一款开源的分布式的版本控制软件 Github:是一个基于git的面向开源及私有软件项目的托管平台 因仅支持git 作为唯 ...
- 关于MySQL数据库——增删改查语句集锦
一.基本的sql语句 CRUD操作:create 创建(添加)read 读取update 修改delete 删除 1.添加数据insert into Info values('p009','张三',1 ...
- [C++]Knights of a Polygonal Table(骑士的多角桌)
[程序结果:用例未完全通过,本博文仅为暂存代码之目的] /* B. Knights of a Polygonal Table url:http://codeforces.com/problemset/ ...