辽宁省实验OJ 235. Sting(manacher+trie)

题目

登录 - Lnsyzx Online Judge

思路来源

辽宁省实验oj官方题解

题解

manacher,对回文这一半的串建个trie树,

manacher初始对半径取min的时候,先倍增当前回文串定位到树上这个深度的位置

然后不断往外扩展时在trie树上扩展,

扩展到不能扩展时,在trie树的叶子上打个异或标记,代表到根的这一条链都需要异或这个标记

由于长串肯定比短串后建,最后倒序往上合并异或标记即可

trie可以被哈希替换,即先预处理串的哈希值,然后对回文串的哈希值建一个树的结构,

仍然对叶子结点打标记,只是由于哈希常数比较大,比较难卡过,只能手写哈希才可以

这题回文树不太能做的原因在于,回文树统计的是以r为结尾的回文串,

回跳时fail跳到最长的回文串上,但是这题异或的贡献在于(l+r)/2,

也就是l在回跳时是会发生改变的,导致信息无法复用

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;

char t[N], s[N << 1], vis[N];
int d[N << 1], fa[N][20], node[N << 1], nex[N][26], cnt = 1, xorsum[N];
// 1 : empty string    0 : -1 srtring
inline int nextnode(int x, char c) {
    if(nex[x][c - 'a']) return nex[x][c - 'a'];
    else {
        nex[x][c - 'a'] = ++cnt; fa[cnt][0] = x;
        for(int i = 1; i < 20; i++) 
            fa[cnt][i] = fa[fa[cnt][i - 1]][i - 1];
        return cnt;
    }
}
inline int getnode(char c) {
    if(c == '*') return 1;
    else return nextnode(0, c);
}
inline int getnode(int mid, int di) {
    // cout << "getnode " << mid << ' ' << d[mid] << ' ' << di << endl;
    if(d[mid] == di) return node[mid];
    int ret = node[mid];
    for(int i = 19, t = (d[mid] - di) / 2; i >= 0; i--)
        if(t & (1 << i)) ret = fa[ret][i];
    return ret;
}

void outnode(int x) {
    printf("%d   fa = %d   xor = %d    ", x, fa[x][0], xorsum[x]);
    for(int i = 0; i < 26 ; i++) {
        if(nex[x][i]) printf("%c,%d  ", i + 'a' , nex[x][i]);

    }
    cout << endl;
}

int main() {
    int T; cin >> T;
    while(T--) {
        int n = 0; scanf("%s", t);
        s[0] = '~', s[1] = '*';
        for(int i = 0; t[i]; i++, n++) {
            s[2 + (i << 1)] = t[i];
            s[3 + (i << 1)] = '*';
        }
        n = n * 2 + 2; s[n] = 0;
        for(int i = 0; i < 20; i++) fa[1][i] = 1;
        for(int i = 1, mid = 0, R = 0, now, td; i < n - 1; i++) {
		    if(i > R) td = 1, now = getnode(s[i]);
		    else td = min(d[mid * 2 - i], R - i + 1), now = getnode(mid * 2 - i, td);
            // cout << endl << i << ' ' << now << ' ' << td << ' ' << d[mid * 2 - i] << endl;
		    while(s[i - td] == s[i + td]) {
                if(s[i - td] != '*') now = nextnode(now, s[i - td]);
                td++;
            }
            // cout << i << ' ' << now << endl;
		    if(i + td > R) mid = i, R = i + td - 1;
            d[i] = td; node[i] = now; 
            if(now > 1) {
                xorsum[now] ^= ((i - 2) >> 1);
                // printf("xorsum[%d] ^= %d\n\n", now, (i - 2) / 2);
            }
	    }
        
        int ans = 0;
        for(int f = cnt; f >= 1; f--){
            xorsum[fa[f][0]] ^= xorsum[f];
            ans=max(ans,xorsum[f]);
        }
        cout << ans << '\n';
        
        if(T) {
            memset(nex, 0, sizeof(nex));
            memset(fa, 0, sizeof(fa));
            memset(xorsum, 0, sizeof(xorsum));
            cnt = 1, vis[0] = vis[1] = 0;
        }
    }
    return 0;
}

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code92007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值