[回文树]JZOJ P3654 回文串

Description

考虑一个只包含小写拉丁字母的符串 s。我们定义 s的一个子串 t的“出现值”为 t在 s中的出现次数乘以t的长度。 请你求出s的所有 回文子串中的最大出现值。

Input

输入只有一行,为一个只包含小写字母 (a−z) 的非空字符串 s。

Output

输出 一个整数,为 所有 回文子串 的最大 出现 值。

Sample Input

输入1:

abacaba

输入2:

www

Sample Output

输出1:

7

输出2:

4

Hint
这里写图片描述
这里写图片描述

题解

    听题时一脸懵逼,只见dalao什么fail指针连来连去的,目光呆滞的看着黑板(其实心里默默问候了出题人全家)
    首先,这是一颗几乎是裸的回文树
!!但是!!
    回文树是什么?又怎么去实现?
    只好找呀,但基本上没几个看懂的
    最后找到一个Victor Wonder dalao%%%
    才勉强对着标看懂
    具体,我也不怎么会说,自行看dalao写的pdf脑补

链接
链接
(第二个比较详细)

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int next[300005][30],fail[300005],len[300005],bz[300005],last,n,tot;;
long long cnt[300005]; 
string s;
void newnode(int l)
{
    ++tot;
    for (int i=1;i<=26;i++) next[tot][i]=0;
    len[tot]=l;
    cnt[tot]=0;
}
int get_fail(int x)
{
    while (bz[n-len[x]-1]!=bz[n]) x=fail[x];
    return x;
}
void add(char x)
{
    int c=x-'a'+1;
    bz[++n]=c;
    int cur=get_fail(last);
    if (!next[cur][c])
    {
        newnode(len[cur]+2);
        fail[tot]=next[get_fail(fail[cur])][c];
        next[cur][c]=tot;
    }
    last=next[cur][c];
    cnt[last]++;
}
void count() { for (int i=tot;i>=0;i--) cnt[fail[i]]+=cnt[i]; }
int main()
{
    freopen("palindrome.in","r",stdin);
    freopen("palindrome.out","w",stdout);
    cin>>s;
    tot=-1; bz[n]=-1; fail[0]=1; 
    newnode(0);
    newnode(-1);
    for (int i=0;i<s.length();i++) add(s[i]);
    count();
    long long ans=0;
    for (int i=2;i<=tot;i++)
    {
        long long k=cnt[i]*len[i];
        if (k>ans) ans=k;
    }
    printf("%lld",ans);
    return 0;
}

转载于:https://www.cnblogs.com/Comfortable/p/8412233.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值