回文树 / 自动机模板

const int maxn = 400000;
const int N = 26 ;

struct Palindromic_Tree {
    int next[maxn][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
    int fail[maxn] ;//fail指针,失配后跳转到fail指针指向的节点
    int cnt[maxn] ;//第 i 号节点表示的回文串出现的次数、注意最后调用 count 函数完成计算
    int num[maxn] ;//以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数(未经验证)
    int len[maxn] ;//len[i]表示节点i表示的回文串的长度
    int S[maxn] ;//存放添加的字符
    int last ;//指向上一个字符所在的节点,方便下一次add
    int n ;//字符数组指针
    int tot ;//节点指针

    int newnode ( int l ) {//新建节点
        for ( int i = 0 ; i < N ; ++ i ) next[tot][i] = 0 ;
        cnt[tot] = 0 ;
        num[tot] = 0 ;
        len[tot] = l ;
        return tot ++ ;
    }

    void init () {//初始化
        tot = 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 ;
    }

    void add ( int c ) {
        c -= 'a' ;
        S[++ n] = c ;
        int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
        if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
            int now = newnode ( len[cur] + 2 ) ;//新建节点
            fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
            next[cur][c] = now ;
            num[now] = num[fail[now]] + 1 ;
        }
        last = next[cur][c] ;
        cnt[last] ++ ;
    }

    void count () {
        for ( int i = tot - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
        //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
    }
}PAM1, PAM2;
单向插入版

 

struct Palindromic_Tree {
    static const int maxn = 400000;
    static const int Letter = 26 ;

    deque<int> text;
    int len[maxn];///len[i]表示节点i表示的回文串的长度
    int cnt[maxn];///第 i 节点表示的回文串出现的次数、注意最后调用 getCnt 函数完成计算
    int num[maxn];///表示以节点 i 代表的回文串端点为端点的不同回文串个数
    int nxt[maxn][Letter];///nxt静态指针和字典树类似,指向的串为当前串两端加上同一个字符构成
    int fail[maxn];///失配指针
    int odd, even;///奇偶原始节点
    int pre, suf;
    int totNode;///自动机所有节点的个数
    LL totNum;///串中所有回文串数量

    int newNode(int _len){
        int ret = totNode++;
        len[ret] = _len;
        cnt[ret] = num[ret] = 0;
        fail[ret] = 0;
        memset(nxt[ret], 0, sizeof(nxt[ret]));
        return ret;
    }

    inline void init(){
        text.clear();
        totNode = 0;
        totNum = 0;
        even = newNode(0);
        odd = newNode(-1);
        fail[even] = fail[odd] = odd;
        pre = suf = even;
    }

    int encode(char c){
        return c - 'a';
    }

    template<typename FunT>
    int getFail(int ch, int cur, FunT getNext){
        for(int i = getNext(len[cur]);
            i < 0 || i >= (int)text.size() || text[i] != ch;
            cur = fail[cur], i = getNext(len[cur]));
        return cur;
    }

    template<typename FunT>
    int Insert(int ch, int last, FunT getNext){
        last = getFail(ch, last, getNext);
        if(!nxt[last][ch]){
            int cur = newNode(len[last] + 2);
            fail[cur] = nxt[getFail(ch, fail[last], getNext)][ch];
            nxt[last][ch] = cur;
            num[cur] = num[fail[cur]] + 1;
        }

        last = nxt[last][ch];
        totNum += (LL)num[last];
        cnt[last]++;
        return last;
    }

    int push_front(char c){
        text.push_front(encode(c));
        pre = Insert(encode(c), pre, [](int i)->int{ return i + 1; });
        if(len[pre] == (int)text.size()) suf = pre;
        return totNode;
    }

    int push_back(char c){
        text.push_back(encode(c));
        suf = Insert(encode(c), suf, [this](int i)->int{ return this->text.size() - i - 2; });
        if(len[suf] == (int)text.size()) pre = suf;
        return totNode;
    }

    int push_front(char *s){
        int ret = 0;
        for(int i=0; s[i]; i++)
            ret = push_front(s[i]);
        return ret;
    }

    int push_back(char *s){
        int ret = 0;
        for(int i=0; s[i]; i++)
            ret = push_back(s[i]);
        return ret;
    }

    inline void getCnt(){
        for(int i=totNode-1; i>=0; i--)
            cnt[fail[i]] += cnt[i];
    }

}PAM;
双向插入版

 

回文树的功能

1.求串S前缀0~i内本质不同回文串的个数

(两个串长度不同或者长度相同且至少有一个字符不同便是本质不同)

对于每一个前缀、创建过程中、自动机节点数 - 2 便是答案

2.求串S内每一个本质不同回文串出现的次数

自动机模板中的 cnt 数组记录的就是这个信息

3.求串S内回文串的个数(其实就是1和2结合起来)

自动机模板中 cnt 数组之和

4.求以下标 i 结尾的回文串的个数

 

 

一个结论

一个串本质不同的回文串数量是 O(n) 级别的

由于回文树上每个节点都表示一个回文串

所以这颗树的节点不会超过串的长度

 

参考博客

电子科大算法讲堂——回文树

posted @ 2018-09-11 17:05  qwerity  阅读(184)  评论(0编辑  收藏  举报