bzoj3676: [Apio2014]回文串

放板子

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=3*1e5+_;

struct Pnode{int w[30],fail,len;};
struct PAM
{
    int n,a[maxn];char ss[maxn];
    Pnode ch[maxn];int cnt,last,num[maxn];
    void insert(int k,int x)
    {
        int pre=last;
        while(a[k]!=a[k-ch[pre].len-1])pre=ch[pre].fail;
        if(ch[pre].w[x]==0)
        {
            int now=++cnt;
            ch[now].len=ch[pre].len+2;
            int ppre=ch[pre].fail;
            while(a[k]!=a[k-ch[ppre].len-1])ppre=ch[ppre].fail;
            ch[now].fail=ch[ppre].w[x];
            ch[pre].w[x]=now;
            
        }
        last=ch[pre].w[x];
        num[last]++;
    }
    void init()
    {
        cnt=1,last=0;
        ch[0].len=0,ch[1].len=-1;
        ch[0].fail=1,ch[1].fail=0;
    }
    void solve()
    {
        init();
        for(int i=1;i<=n;i++)
        insert(i,a[i]);
        for(int i=cnt;i>=2;i--)num[ch[i].fail]+=num[i];
        
        LL ans=0;
        for(int i=2;i<=cnt;i++)ans=max(ans,(LL)num[i]*ch[i].len);
        printf("%lld\n",ans);
    }
    void main()
    {
        scanf("%s",ss+1);n=strlen(ss+1);
        for(int i=1;i<=n;i++)a[i]=ss[i]-'a'+1;
        solve();
    }
}P;

int main()
{
    P.main();
    return 0;
}

 

posted @ 2019-03-07 10:54  AKCqhzdy  阅读(69)  评论(0编辑  收藏  举报