题解-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 只做出来四道比较水的题目,还需要加强中等题的训练 ...
随机推荐
- JAVA核心技术I---JAVA基础知识(格式化相关类)
一:格式化相关类 (一)java.text包java.text.Format的子类 –NumberFormat:数字格式化,抽象类 DecimalFormat –MessageFormat:字符串格式 ...
- 设计模式---接口隔离模式之代理模式(Proxy)
一:概念 代理模式(Proxy Pattern)就是为其他对象提供一种代理以控制对这个对象的访问. 比如: 智能指针 为别人做嫁衣 所谓代理,是指具有与代理元(被代理的对象)具有相同的接口的类,客户端 ...
- Linux记录-使用python临时搭建web服务器
python2: python -m SimpleHTTPServer 8888 python3: python -m http.server 8888 wget ip:8888/文件
- spring+springmvc+ibatis整合注解方式实例【转】
源自-----> http://shaohan126448.iteye.com/blog/2033563 (1)web.xml文件(Tomcat使用) 服务器根据配置内容初始化spring框架, ...
- VScode 1.13 gocode提示dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected..
在将VScode升级至 1.13后让升级gocode,在升级时报出如下错误 D:\go_work\src>go get -u -v github.com/mdempsky/gocode gith ...
- js中html拼接
https://i.cnblogs.com/EditPosts.aspx?postid=10620765&update=1
- 【概括】C++11/14特性
c++11 c++14
- overlay网络隔离实验失败记录
按照 https://www.cnblogs.com/CloudMan6/p/7341487.html进行操作,实验结果与老师文章中的正好相反,不同 overlay 网络中的主机可以通信,验证部分见下 ...
- Sql分页代码示例
select * from (select ROW_NUMBER()over( order by id) orderid,* from test) a where a.orderid between ...
- 数据库面试题之COUNT(*),COUNT(字段),CONUT(DISTINCT 字段)的区别
COUNT(*).明确的返回数据表中的数据个数,是最准确的 COUNT(列),返回数据表中的数据个数,不统计值为null的字段 COUNT(DISTINCT 字段) 返回数据表中不重复的的数据个数,不 ...