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;
    }
    
  • 相关阅读:
    【转载】大连商品交易所-套利交易相关问题
    LC 1340. Jump Game V
    1057 Stack
    1059 Prime Factors
    LC 1425. Constrained Subset Sum
    LCP 13. 寻宝
    P3381 【模板】最小费用最大流
    P3376 【模板】网络最大流
    LC 面试题51. 数组中的逆序对
    LC 466. Count The Repetitions
  • 原文地址:https://www.cnblogs.com/BakaCirno/p/12310422.html
Copyright © 2011-2022 走看看