【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;
}

最新文章

  1. arcgis操作笔记-根据属性提取某区域要素
  2. stl 存放对象析构问题
  3. MHA安装手记
  4. ASp.net 注册
  5. python下的复杂网络编程包networkx的安装及使用
  6. Entity Framework学习 - 3.关联查询
  7. root-systerm-bin是什么program
  8. 图像库---Image Datasets---OpenSift源代码---openSurf源代码
  9. 搭建SSH
  10. (转)Hadoop MapReduce链式实践--ChainReducer
  11. 转:MySQL表名不区分大小写
  12. win10 .net3.5的问题及解决方案
  13. rabbitMQ教程(五)rabbitmq 指令 以及解决web管理界面无法使用guest用户登录
  14. commons-lang
  15. CAN协议,系统结构和帧结构
  16. (13)Python文件操作
  17. day 32 子进程的开启 及其用法
  18. T-SQL编程中的异常处理-异常捕获(try catch)与抛出异常(throw)
  19. Python使用filetype精确判断文件类型 (文件类型获取)
  20. C# ABP WebApi与Swagger UI的集成

热门文章

  1. 将 Python3 文件打包成 exe 文件
  2. 阿里云服务器Centos上Apache安装SSL证书配置Https
  3. Javascript深入__proto__和prototype的区别和联系
  4. rz和sz上传下载文件
  5. 华为笔试——C++的int型数字位排序
  6. C++ 根据图片url 批量 下载图片
  7. python request 获取cookies value值的方法
  8. loadrunner socket协议问题归纳(6)
  9. 跳蚤APP
  10. Ubuntu16.04安装json-c