回文自动机

详细博客待更~
这里先写模板

struct PAM_T{
    int ch[26],fail,len,num,trans;
};
struct PAM{
    PAM_T b[N];
    char s[N];
    int cur,n,cnt;
    PAM(){
        b[0].len = 0,b[1].len = -1;
        b[0].fail = 1,b[1].fail = 0;
        cnt = 1;
        cur = 0;
    }
    void read(){
        scanf("%s",s + 1);
    }
    int getfail(int x){
        while(s[n - b[x].len - 1] == s[n]) x = b[x].fail;
        return x;
    }

    void insert(){
        int p = getfail(cur);
        int x = s[n] - 'a';
        if(!b[p].ch[x]){
            b[++ cnt].len = b[p].len + 1;
            int tmp = getfail(b[p].fail);
            b[cnt].fail = b[tmp].ch[x];
            b[p].ch[x] = cnt;
            if(b[cnt].len <= 2) b[cnt].trans = b[cnt].fail;
            else{
                int tmp = b[p].trans;
                while(s[n - b[tmp].len - 1] != s[n] || (b[tmp].len + 2) * 2 > b[cnt].len) tmp = b[tmp].fail;
                b[cnt].trans = b[tmp].ch[x];
            }
        }
        cur = b[p].ch[x];
        b[cur].num ++;
    }
}P;
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页