BZOJ 2555 SubString 后缀自动机+LCT

在线向原串尾添加字符,询问字符串在原串中的出现次数。

发现每次添加字符,Right集合就会沿Parent树大小+1,考虑LCT维护Parent树,以便区间修改。

14.000s。。。。

#include <cstdio>
#include <cstring>
const int N = 1200005, rt = 1;
char s[3000010];
struct LCT {
    int c[N][2], sz[N], fa[N], lazy[N], sk[N];
    void add(int x, int z) { sz[x] += z; lazy[x] += z; }
    bool root(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; }
    void link(int x, int y) { access(y); splay(y); fa[x] = y; add(y, sz[x]); }
    void cut(int x, int y) { access(y); splay(x); fa[x] = 0; add(y, -sz[x]); }   
    void access(int x) {
        for (int t = 0; x; t = x, x = fa[x])
            splay(x), c[x][1] = t, update(x);
    }
    void update(int x) {
        if (lazy[x] == 0) return;
        add(c[x][0], lazy[x]);
        add(c[x][1], lazy[x]);
        lazy[x] = 0;
    }
    void rotate(int x,int k) {
        int y = fa[x], z = fa[y];
        c[y][k ^ 1] = c[x][k];
        fa[c[x][k]] = y; c[x][k] = y;
        if (c[z][0] == y) c[z][0] = x;
        else if (c[z][1] == y) c[z][1] = x;
        fa[y] = x; fa[x] = z;
        update(y); update(x);
    }
    void splay(int x) {
        int i, top = 0; sk[++top] = x;
        for (i = x; !root(i); i = fa[i]) sk[++top] = fa[i];
        while (top) update(sk[top--]);
        while (!root(x)) {
            int y = fa[x], z = fa[y];
                 if (root(y) && c[y][0] == x) rotate(x, 1);
            else if (root(y) && c[y][1] == x) rotate(x, 0);
            else if (c[z][0] == y && c[y][0] == x) rotate(y, 1), rotate(x, 1);
            else if (c[z][0] == y && c[y][1] == x) rotate(x, 0), rotate(x, 1);
            else if (c[z][1] == y && c[y][0] == x) rotate(x, 1), rotate(x, 0);
            else rotate(y, 0), rotate(x, 0);
        }
    }
} lct;

struct SAM {
    int ch[N][26], fa[N], ma[N], cnt, last;
    SAM() : cnt(1), last(1) { }
    void add(char c) {
        int np = ++cnt, p = last; last = np; ma[np] = ma[p] + 1;
        while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
        if (!p) fa[np] = rt;
        else {
            int q = ch[p][c];
            if (ma[q] == ma[p] + 1) fa[np] = q;
            else {
                int nq = ++cnt; ma[nq] = ma[p] + 1;
                memcpy(ch[nq], ch[q], sizeof ch[q]);
                fa[nq] = fa[q]; lct.link(nq, fa[q]); lct.cut(q, fa[q]);
                fa[q] = fa[np] = nq; lct.link(q, nq);
                while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
            }
        }
        lct.sz[np] = 1; lct.link(np, fa[np]);
    }
    int query(char *s) {
        int p = 1, i;
        for (i = 0; s[i]; ++i) {
            if (ch[p][s[i] - 'A'] == 0) return 0;
            p = ch[p][s[i] - 'A'];
        }
        lct.access(p); lct.splay(p);
        return lct.sz[p];
    }
} sam;

void decodeWithMask(char *s, int mask) {
    int n = strlen(s), i, t;
    for (i = 0; i < n; ++i) {
        mask = (mask * 131 + i) % n;
        t = s[i]; s[i] = s[mask]; s[mask] = t;
    }
}

int main() {
    int mask = 0, i, t, ans; char op[8];
    scanf("%d%s", &t, s);
    for (i = 0; s[i]; ++i) sam.add(s[i] - 'A');
    while (t--) {
        scanf("%s%s", op, s);
        decodeWithMask(s, mask);
        if (op[0] == 'A') for (i = 0; s[i]; ++i) sam.add(s[i] - 'A');
        else printf("%d\n", ans = sam.query(s)), mask ^= ans;
    }
    return 0;
}

2555: SubString

Description

懒得写背景了,给你一个字符串init,要求你支持两个操作
1. 在当前字符串的后面插入一个字符串
2. 询问字符串s在当前字符串中出现了几次?(作为连续子串)
你必须在线支持这些操作:

void decodeWithMask(char *s, int mask)
{
    int n = strlen(s), i, t;
    for (i = 0; i < n; ++i)
    {
        mask = (mask * 131 + i) % n;
        t = s[i]; s[i] = s[mask]; s[mask] = t;
    }
}

Input

第一行一个数Q表示操作个数

第二行一个字符串表示初始字符串init

接下来Q行,每行2个字符串Type,Str 

Type是ADD的话表示在后面插入字符串。

Type是QUERY的话表示询问某字符串在当前字符串中出现了几次。

为了体现在线操作,你需要维护一个变量mask,初始值为0


读入串Str之后,使用这个过程将之解码成真正询问的串TrueStr。
询问的时候,对TrueStr询问后输出一行答案Result
然后mask = mask xor Result  
插入的时候,将TrueStr插到当前字符串后面即可。

HINT:ADD和QUERY操作的字符串都需要解压

Output

Sample Input

2



A



QUERY B



ADD BBABBBBAAB

Sample Output

0

HINT

40 % 的数据字符串最终长度 <= 20000,询问次数<= 1000,询问总长度<= 10000

100 % 的数据字符串最终长度 <= 600000,询问次数<= 10000,询问总长度<= 3000000

新加数据一组–2015.05.20

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值