【BZOJ2565】最长双回文串(回文树)

本文介绍了一种寻找字符串中最长双回文子串的方法,利用正反向构造的后缀自动机(PAM)来高效解决问题。通过构建两个方向的回文树,可以找到由两个回文子串组成的最长子串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

顺序和逆序读起来完全一样的串叫做回文串。比如acbca是回文串,而abc不是(abc的顺序为“abc”,逆序为“cba”,不相同)。
输入长度为n的串S,求S的最长双回文子串T,即可将T分为两部分X,Y,(|X|,|Y|≥1)且X和Y都是回文串。


Solution

其实可以直接上Manacher233

正反各建一个PAM,记录每次插入每个字符后的len,将两个PAM对应位置的答案加起来求最大值即可。


Code

/************************************************
 * Au: Hany01
 * Date: Jun 22nd, 2018
 * Prob: [BZOJ2565] 最长双回文串
 * Email: hany01@foxmail.com
 * Institute: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 100005;

struct PalindromicTree
{
    int len[maxn], fa[maxn], ch[maxn][26], mx[maxn], tot, las, n;
    char s[maxn];

    inline void extend(int i)
    {
        int p = las, c = s[i] - 97;
        while (s[i - len[p] - 1] != s[i]) p = fa[p];
        if (!ch[p][c]) {
            int np = ++ tot, k = fa[p];
            while (s[i - len[k] - 1] != s[i]) k = fa[k];
            len[np] = len[p] + 2, fa[np] = ch[k][c], ch[p][c] = np;
        }
        mx[i] = len[las = ch[p][c]];
    }

    inline void build()
    {
        fa[0] = fa[1] = 1, len[tot = 1] = -1;
        For(i, 1, n) extend(i);
    }

}PT1, PT2;

int main()
{
#ifdef hany01
    File("bzoj2565");
#endif

    static int Ans;

    scanf("%s", PT1.s + 1), PT1.n = PT2.n = strlen(PT1.s + 1);
    Cpy(PT2.s, PT1.s), reverse(PT2.s + 1, PT2.s + 1 + PT1.n);

    PT1.build(), PT2.build();
    For(i, 1, PT1.n - 1) chkmax(Ans, PT1.mx[i] + PT2.mx[PT1.n - i]);
    printf("%d\n", Ans);

    return 0;
}
//春日游,杏花吹满头。
//    -- 韦庄《思帝乡·春日游》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值