zoukankan      html  css  js  c++  java
  • 回文树模板

    回顾了一下回文树
    回文树可以统计一个字符串中本质不同的回文串的数量、长度、回文串的总数量等

    反正以后遇到回文串的题直接上回文树就好了

    模板题
    这个题就直接在最后累计回文串个数的时候顺便统计存在值就行了,十分方便
    在出这道题的时候回文树还没有提出,可以发现之前大家想做这道题得吃不少苦,
    有了回文树就变成板子题了,可见回文树是解决回文串问题的利器

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int MAXN=3e5+10;
    char s[MAXN];
    LL ans=0;
    
    struct Pam{
        char s[MAXN];
        int n,tr[MAXN][26],tot,last,fail[MAXN],len[MAXN],cnt[MAXN];
        Pam(){
            s[n=tot=last=0]=0;
            memset(tr,0,sizeof(tr));
            len[tot++]=0;
            len[tot++]=-1;
            fail[0]=1;
        }
        inline int getfail(int x){
            while(s[n-len[x]-1]!=s[n]) x=fail[x];
            return x;
        }
        void insert(char c){
            c-='a';s[++n]=c;
            int cur=getfail(last);
            if(!tr[cur][c]){
                int now=tot++;len[now]=len[cur]+2;
                fail[now]=tr[getfail(fail[cur])][c];
                tr[cur][c]=now;
            }
            last=tr[cur][c];
            cnt[last]++;
        }
        void count(){
            for(int i=tot-1;i>=0;i--) cnt[fail[i]]+=cnt[i];
            for(int i=0;i<tot;i++) ans=max(ans,1ll*len[i]*cnt[i]);
        }
    }pam;
    
    int main(){
    #ifndef ONLINE_JUDGE
        freopen("data.in","r",stdin);
        freopen("data.out","w",stdout);
    #endif
        scanf("%s",s+1);
        int n=strlen(s+1);
        for(int i=1;i<=n;i++) pam.insert(s[i]);
        pam.count();
        printf("%lld
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    1017.陶陶装苹果
    1084.爬楼梯加强版
    1056.A ^ B Problem 快速幂算法。
    1074.我们喜欢递归的斐波那契数列
    1073.我们喜欢递归的阶乘
    1046 没过
    python 基本常用数据类型
    yii2.0 数据库查询操作
    python 随便笔记
    搭建自己的koa+mysql后台模板
  • 原文地址:https://www.cnblogs.com/BakaCirno/p/12310422.html
Copyright © 2011-2022 走看看