【CF700E】Cool Slogans(后缀自动机)

题面

洛谷

CodeForces

题解

构建后缀自动机,求出后缀树

现在有个比较明显的\(dp\)

设\(f[i]\)表示从上而下到达当前点能够满足条件的最优值

只需要检查父亲节点是否在当前串中出现过两次就行了

这个判断用\(endpos\)来判断

如果出现过超过两次,那么在当前点所掌控的任意一个\(endpos\)以及前面的区间中

必定出现了超过两次

用一个线段树合并求\(endpos\)集合

然后计算一下出现次数就好了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 444444
char ch[MAX];
int n,last=1,tot=1,TOT;
struct SegNode{int ls,rs;}st[MAX*25];
int rt[MAX],f[MAX],ans=1;
void Modify(int &x,int l,int r,int p)
{
if(!x)x=++TOT;if(l==r)return;
int mid=(l+r)>>1;
if(p<=mid)Modify(st[x].ls,l,mid,p);
else Modify(st[x].rs,mid+1,r,p);
}
int Merge(int x,int y)
{
if(!x||!y)return x|y;
int z=++TOT;
st[z].ls=Merge(st[x].ls,st[y].ls);
st[z].rs=Merge(st[x].rs,st[y].rs);
return z;
}
int Query(int x,int l,int r,int L,int R)
{
if(!x)return 0;if(L<=l&&r<=R)return 1;
int mid=(l+r)>>1;
if(L<=mid&&Query(st[x].ls,l,mid,L,R))return 1;
if(R>mid&&Query(st[x].rs,mid+1,r,L,R))return 1;
return 0;
}
struct Node{int ff,len,pos,son[26];}t[MAX];
void extend(int c,int pos)
{
int p=last,np=++tot;last=np;t[np].pos=pos;
t[np].len=t[p].len+1;
while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
if(!p)t[np].ff=1;
else
{
int q=t[p].son[c];
if(t[q].len==t[p].len+1)t[np].ff=q;
else
{
int nq=++tot;
t[nq]=t[q];t[nq].len=t[p].len+1;
t[q].ff=t[np].ff=nq;
while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
}
}
}
int a[MAX],p[MAX],top[MAX];
int main()
{
scanf("%d",&n);scanf("%s",ch+1);
for(int i=1;i<=n;++i)extend(ch[i]-97,i),Modify(rt[last],1,n,i);
for(int i=1;i<=tot;++i)a[t[i].len]++;
for(int i=1;i<=n;++i)a[i]+=a[i-1];
for(int i=tot;i>=1;--i)p[a[t[i].len]--]=i;
for(int i=tot;i>1;--i)rt[t[p[i]].ff]=Merge(rt[t[p[i]].ff],rt[p[i]]);
for(int i=2;i<=tot;++i)
{
int u=p[i],fa=t[u].ff;
if(fa==1){f[u]=1,top[u]=u;continue;}
int x=Query(rt[top[fa]],1,n,t[u].pos-t[u].len+t[top[fa]].len,t[u].pos-1);
if(x)f[u]=f[fa]+1,top[u]=u;
else f[u]=f[fa],top[u]=top[fa];
ans=max(ans,f[u]);
}
printf("%d\n",ans);
return 0;
}

【CF700E】Cool Slogans(后缀自动机)的更多相关文章

  1. CF700E Cool Slogans 后缀自动机 + right集合线段树合并 + 树形DP

    题目描述 给出一个长度为n的字符串s[1],由小写字母组成.定义一个字符串序列s[1....k],满足性质:s[i]在s[i-1] (i>=2)中出现至少两次(位置可重叠),问最大的k是多少,使 ...

  2. 【CF700E】Cool Slogans 后缀自动机+线段树合并

    [CF700E]Cool Slogans 题意:给你一个字符串S,求一个最长的字符串序列$s_1,s_2,...,s_k$,满足$\forall s_i$是S的子串,且$s_i$在$s_{i-1}$里 ...

  3. Codeforces.700E.Cool Slogans(后缀自动机 线段树合并 DP)

    题目链接 \(Description\) 给定一个字符串\(s[1]\).一个字符串序列\(s[\ ]\)满足\(s[i]\)至少在\(s[i-1]\)中出现过两次(\(i\geq 2\)).求最大的 ...

  4. Codeforces Round #364 (Div. 1) (差一个后缀自动机)

    B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...

  5. CF700E Cool Slogans

    CF700E Cool Slogans 题目描述 给出一个长度为n的字符串\(s[1]\),由小写字母组成.定义一个字符串序列\(s[1....k]\),满足性质:\(s[i]\)在\(s[i-1] ...

  6. 字符串数据结构模板/题单(后缀数组,后缀自动机,LCP,后缀平衡树,回文自动机)

    模板 后缀数组 #include<bits/stdc++.h> #define R register int using namespace std; const int N=1e6+9; ...

  7. SAM(后缀自动机)总结

    “写sam是肯定会去写的,这样才学的了字符串,后缀数组又不会用 >ω<, sam套上数据结构的感觉就像回家一样! 里面又能剖分又能线段树合并,调试又好调,我爱死这种写法了 !qwq” SA ...

  8. BZOJ 后缀自动机四·重复旋律7

    后缀自动机四·重复旋律7 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 神奇的 ...

  9. 【Codeforces235C】Cyclical Quest 后缀自动机

    C. Cyclical Quest time limit per test:3 seconds memory limit per test:512 megabytes input:standard i ...

随机推荐

  1. HBase第三章 过滤器

    1  列值过滤器 SingleColumnValueFilter 对列值进行过滤. @Test public void scanDataByFilter() throws IOException { ...

  2. MySQLdb in Python: “Can't connect to MySQL server on 'localhost'”

    因为我使用的是win64,所以在此系统下,需要设置为 127.0.0.1 #coding=utf-8 import MySQLdb if __name__ == '__main__': # 打开数据库 ...

  3. php 批量载入文件的几种方式

    方式1:spl_autoload_register // Register the autoloader. /** * Contains the functionality for auto-load ...

  4. Intellij IDEA 2017 通过scala工程运行wordcount

    首先是安装scala插件,可以通过idea内置的自动安装方式进行,也可以手动下载可用的插件包之后再通过idea导入. scala插件安装完成之后,新建scala项目,右侧使用默认的sbt 点击Next ...

  5. Hexo+gitment

    Gitment是一个基于GitHub问题的评论系统,可以在没有任何服务器端实现的前端使用. 演示页面 中文简介 特征 入门 方法 定制 关于安全 特征 GitHub登录 Markdown / GFM支 ...

  6. 使用Photon引擎进行unity网络游戏开发(一)——Photon引擎简介

    使用Photon引擎进行unity网络游戏开发(一)--Photon引擎简介 Photon PUN Unity 网络游戏开发 Photon引擎简介: 1. 服务器引擎: 服 务 器 引 擎 介 绍 服 ...

  7. JavaScript学习笔记(一)——JS速览

    第一章 JS速览 1 限制时间处理事件 <script> setTomeout(wakeUpUser,5000); function wakeUpUser() { alert(" ...

  8. Harbor配置https,并安装内容信任插件(notary)

    1.配置https https://github.com/goharbor/harbor/blob/master/docs/configure_https.md 2.harbor安装notary插件 ...

  9. python基础-02-while格式化逻辑运算

    python其他知识目录 1.循环打印“我是小马过河” while True:    print('我是小马过河') #4.用while从一打印到10 #5.请通过循环,1 2 3 4 5 6 8 9 ...

  10. Thunder团队——bug修正

    团队:欢迎来怼 发现的问题: 1.首先用户通过爱阅APP内部的网址跳转到各大电子书网站时,需要额外启动手机自身浏览器:就以豆瓣网为例,阅读豆瓣网上的一些书籍,是跳转到手机自带浏览器的,APP内部提供的 ...