TsinsenA1280/BZOJ2565 最长双回文串(回文树)

题意:输入长度为n的串S,求S的最长双回文子串T,即可将T分为两部分X,Y,(|X|,|Y|≥1)且X和Y都是回文串。2≤|S|≤10^5。

 

思路:因为求的是双回文串的最大长度,优先找最长的X或最长的Y会出问题。所以要枚举X和Y的分界点,枚举每个位置,找出当前位置向前和向后能延伸到的最长回文串,求和更新答案。

显然回文自动机可以求出以每个点为末尾向前能够延伸到的最大回文串,要解决的是求一个点能够向后延伸到的最大回文串,我们将字符串倒转,同样处理一次即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 100005;
const int mod = 1000000007;
const int N = 27; // 字符集大小
struct Palindromic_Tree {
    int nxt[maxn][N];//nxt指针,nxt指针和字典树类似,指向的串为当前串两端加上同一个字符构成
    int fail[maxn];//fail指针,失配后跳转到fail指针指向的节点
    int cnt[maxn];//cnt[i]表示i代表的本质不同的串的个数 //结点i代表的回文串在原串中出现的次数
    int num[maxn];//以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数
    int len[maxn];//len[i]表示节点i表示的回文串的长度
    int S[maxn];//存放添加的字符
    int last;//指向上一个字符所在的节点,方便下一次add
    int n;//字符数组指针
    int p;//节点指针/节点数
    int newnode(int l) {//新建节点
        for(int i = 0; i < N; ++i) nxt[p][i] = 0;
        cnt[p] = 0;
        num[p] = 0;
        len[p] = l;
        return p++;
    }
    void init() {//初始化
        p = 0;
        newnode(0);
        newnode(-1);
        last = 0;
        n = 0;
        S[n] = -1;//开头放一个字符集中没有的字符,减少特判
        fail[0] = 1;
    }
    int get_fail(int x) {//和KMP一样,失配后找一个尽量最长的
        while (S[n - len[x] - 1] != S[n]) x = fail[x];
        return x;
    }
    int add(int c) {
        //c -= 'a';
        S[++n] = c;
        int cur = get_fail(last);//通过上一个回文串找这个回文串的匹配位置
        if (!nxt[cur][c]) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
            int now = newnode(len[cur] + 2);//新建节点
            fail[now] = nxt[get_fail(fail[cur])][c];//和AC自动机一样建立fail指针,以便失配后跳转
            nxt[cur][c] = now;
            num[now] = num[fail[now]] + 1;
        }
        last = nxt[cur][c];
        cnt[last]++;
        return last; //以添加的字符为后缀构成的最大回文串所在的节点
    }
    void cont() {
        for (int i = p - 1; i >= 0; --i) cnt[fail[i]] += cnt[i];
        //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
    }
}p_tree;
char s[maxn];
int l1[maxn], l2[maxn], ans;
int main() {
    scanf("%s", s + 1);
    ans = l1[0] = l2[0] = 0;
    p_tree.init();
    int len = strlen(s + 1);
    for (int i = 1; i <= len; ++i) {
        l1[i] = p_tree.len[p_tree.add(s[i] - 'a')];
    }
    // 倒着再处理一边
    p_tree.init();
    for (int i = len; i >= 1; --i) {
        l2[i] = p_tree.len[p_tree.add(s[i] - 'a')];
        if (l2[i] + l1[i - 1] > ans) { // 两段拼接求长度
            ans = l2[i] +l1[i - 1];
        }
    }
    printf("%d\n", ans);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值