回文树1960. Palindromes and Super Abilities

Bryce1010模板

http://acm.timus.ru/problem.aspx?space=1&num=1960
这里写图片描述

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
struct PalindromicTree{
    int fail[maxn],len[maxn],son[maxn][26];
    int tot,last,n;
    char s[maxn];
    int newnode(int slen = 0){
        memset(son[tot],0,sizeof son[tot]);
        len[tot] = slen;
        return tot++;
    }
    void init(){
        n = tot = last = 0;
        newnode(0);
        newnode(-1);
        fail[1] = fail[0] = 1;
        s[n] = -1;
    }
    int getFail(int x){
        while(s[n - len[x] - 1] != s[n]) x = fail[x];
        return x;
    }
    void extend(int c){
        s[++n] = c;
        int cur = getFail(last);
        if(!son[cur][c]){
            int x = newnode(len[cur] + 2);
            fail[x] = son[getFail(fail[cur])][c];
            son[cur][c] = x;
        }
        last = son[cur][c];
    }
}pt;
char str[maxn];
int main(){
    while(~scanf("%s",str)){
        pt.init();
        bool flag = false;
        for(int i = 0; str[i]; ++i){
            pt.extend(str[i] - 'a');
            if(flag) putchar(' ');
            printf("%d",pt.tot - 2);
            flag = true;
        }
        putchar('\n');
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Fire_to_cheat_/article/details/81052348
今日推荐