后缀自动机构造(暂存)

struct SAM {
    struct Node{
        int next[27];
        int fa, len;
        void init() {
            fa = len = 0;
            mes(next, 0);
        }
    } node[maxn<<1];
    int sz, last;
    void init() {
        sz = last = 1;
        node[sz].init();
    }
    void insert(int x) {
        int p = last, np = last = ++sz;
        node[np].init();
        node[np].len = node[p].len + 1;
        for(; p&&!node[p].next[x]; p=node[p].fa)    node[p].next[x] = np;
        if(p == 0) {
            node[np].fa = 1;
        } else {
            int q = node[p].next[x];
            if(node[q].len == node[p].len+1) {
                node[np].fa = q;
            } else {
                int nq = ++sz;
                node[nq] = node[q];
                node[np].fa = node[q].fa = nq;
                node[nq].len = node[p].len + 1;
                for(; p&&node[p].next[x]==q; p=node[p].fa)  node[p].next[x] = nq;
            }
        }
    }
} sam;

猜你喜欢

转载自www.cnblogs.com/Jiaaaaaaaqi/p/10867235.html