bzoj3676 回文串【回文树】

解题思路:

回文树模板题,建好后取每个节点cnt*len的最大值即可。
回文树解析详见:http://blog.csdn.net/u013368721/article/details/42100363
真的好文。

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int getint()
{
    int i=0,f=1;char c;
    for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());
    if(c=='-')c=getchar(),f=-1;
    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
    return i*f;
}

const int N=300005;
struct node
{
    int son[26],fail,cnt,len;
}tr[N];
int n,tot=-1,last;
char s[N];
ll ans;

int newnode(int l)
{
    tr[++tot].len=l;
    return tot;
}

void Init()
{
    newnode(0),newnode(-1);
    last=0,tr[0].fail=1,s[0]='#';
}

int getfail(int x)
{
    while(s[n-tr[x].len-1]!=s[n])x=tr[x].fail;
    return x;
}

void Insert(char c)
{
    s[++n]=c;
    int cur=getfail(last);
    if(!tr[cur].son[c-'a'])
    {
        int now=newnode(tr[cur].len+2);
        tr[now].fail=tr[getfail(tr[cur].fail)].son[c-'a'];
        tr[cur].son[c-'a']=now;
    }
    last=tr[cur].son[c-'a'],tr[last].cnt++;
}

int main()
{
    //freopen("lx.in","r",stdin);
    char c;
    Init();
    for(c=getchar();c<'a'||c>'z';c=getchar());
    while(c>='a'&&c<='z')
        Insert(c),c=getchar();
    for(int i=tot;i>=0;i--)tr[tr[i].fail].cnt+=tr[i].cnt;
    for(int i=2;i<=tot;i++)ans=max(ans,1ll*tr[i].len*tr[i].cnt);
    cout<<ans<<'\n';
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值