码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷P3804 - 【模板】后缀自动机

时间:2018-03-24 00:46:43      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:int   gpo   std   ons   href   problem   div   amp   const   

Portal

Description

模板题啦~
推荐WJMZBMR在WC2012上的讲义《后缀自动机》

Code

//【模板】后缀自动机
#include <cstdio>
int max(int x,int y) {return x>y?x:y;}
int const N=2e6+10;
int n; char s[N];
int rt,ndCnt,last;
int fa[N],ch[N][26],len[N],siz[N];
void ins(int x)
{
    int p=last,np=++ndCnt;
    last=np,len[np]=len[p]+1; siz[np]=1;
    for(p;!ch[p][x]&&p;p=fa[p]) ch[p][x]=np;
    if(!p) {fa[np]=rt; return;}
    int q=ch[p][x];
    if(len[p]+1==len[q]) {fa[np]=q; return;}
    int nq=++ndCnt; len[nq]=len[p]+1;
    for(int i=0;i<26;i++) ch[nq][i]=ch[q][i];
    fa[nq]=fa[q]; fa[q]=fa[np]=nq;
    for(p;ch[p][x]==q;p=fa[p]) ch[p][x]=nq;
}
int cnt[N],t[N];
int query()
{
    for(int i=1;i<=ndCnt;i++) cnt[len[i]]++;
    for(int i=1;i<=ndCnt;i++) cnt[i]+=cnt[i-1];
    for(int i=1;i<=ndCnt;i++) t[cnt[len[i]]--]=i;
    int res=0;
    for(int i=ndCnt;i>=1;i--)
    {
        int p=t[i];
        siz[fa[p]]+=siz[p];
        if(siz[p]>1) res=max(res,1LL*siz[p]*len[p]);
    }
    return res;
}
int main()
{
    scanf("%s",s+1);
    last=1,rt=++ndCnt;
    for(n=1;s[n];n++) ins(s[n]-'a');
    printf("%lld\n",query());
    return 0;
}

洛谷P3804 - 【模板】后缀自动机

标签:int   gpo   std   ons   href   problem   div   amp   const   

原文地址:https://www.cnblogs.com/VisJiao/p/LgP3804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!