【BZOJ2555】SubString(SAM,LCT)

19 篇文章 0 订阅
11 篇文章 0 订阅

Description

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


Solution

用SAM维护出现次数,由于要兹磁动态加字符串,用LCT维护子树内的size和即可。


Code

/************************************************
 * Au: Hany01
 * Date: Jun 25th, 2018
 * Prob: BZOJ2555 Substring
 * Email: hany01@foxmail.com
 * Institute: Yali High School
************************************************/

#include<bits/stdc++.h>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 1200005;

char s[maxn * 5];
int ch[maxn][26], fa[maxn], len[maxn * 5], sz[maxn * 5], tot = 1, n, las = 1, mask;

struct LCT
{
    int sum[maxn], fa[maxn], ch[maxn][2], stk[maxn], top, rev[maxn], imag[maxn];

#define dir(x) (ch[fa[x]][1] == x)
#define isrt(x) (ch[fa[x]][1] != x && ch[fa[x]][0] != x)

    inline void maintain(int u) { sum[u] = sum[ch[u][0]] + sum[ch[u][1]] + sz[u] + imag[u]; }

    inline void pushdown(int u) {
        if (rev[u]) rev[u] = 0, swap(ch[u][0], ch[u][1]), rev[ch[u][0]] ^= 1, rev[ch[u][1]] ^= 1;
    }

    inline void rotate(int u) {
        int f = fa[u], gf = fa[f], d = dir(u);
        fa[ch[f][d] = ch[u][d ^ 1]] = f, fa[u] = gf;
        if (!isrt(f)) ch[gf][dir(f)] = u;
        ch[fa[f] = u][d ^ 1] = f, maintain(f), maintain(u);
    }

    inline void splay(int u) {
        stk[top = 1] = u;
        for (int t = u; !isrt(t); t = fa[t]) stk[++ top] = fa[t];
        while (top) pushdown(stk[top --]);
        for ( ; !isrt(u); rotate(u)) if (!isrt(fa[u])) rotate(dir(u) == dir(fa[u]) ? fa[u] : u);
    }

    inline void access(int u) {
        for (int t = 0; u; t = u, u = fa[u]) splay(u), imag[u] += sum[ch[u][1]] - sum[t], ch[u][1] = t, maintain(u);
    }

    inline void makeroot(int u) { access(u), splay(u), rev[u] ^= 1; }

    inline void cut(int u, int v) {
        makeroot(u), access(v), splay(v), fa[u] = ch[v][0] = 0, maintain(v);
    }

    inline void link(int u, int v) {  makeroot(u), access(v), splay(v), imag[v] += sum[u], fa[u] = v; maintain(v); }

}lct;

inline void decode(char *s, int n, int mask) {
    rep(j, n) swap(s[j + 1], s[(mask = (mask * 131 + j) % n) + 1]);
}

inline void extend(int c)
{
    int np = ++ tot, p = las;
    len[las = np] = len[p] + 1, sz[np] = 1;
    while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
    if (!p) fa[np] = 1, lct.link(1, np);
    else {
        int q = ch[p][c];
        if (len[q] == len[p] + 1) fa[np] = q, lct.link(np, q);
        else {
            int nq = ++ tot;
            lct.link(nq, fa[q]), fa[nq] = fa[q], Cpy(ch[nq], ch[q]);
            len[nq] = len[p] + 1, lct.cut(q, fa[q]), fa[q] = fa[np] = nq, lct.link(q, nq), lct.link(np, nq);
            while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    File("bzoj2555");
#endif
    static int T = read();
    static char ty[7];

    scanf("%s", s + 1), n = strlen(s + 1);
    For(i, 1, n) extend(s[i] - 65);

    while (T --) {
        scanf("%s", ty), scanf("%s", s + 1), n = strlen(s + 1), decode(s, n, mask);
        if (ty[0] == 'A') For(i, 1, n) extend(s[i] - 65);
        else {
            register int u = 1, now = 0, c, fl = 1;
            For(i, 1, n) {
                c = s[i] - 65;
                if (ch[u][c]) u = ch[u][c], ++ now;
                else { fl = 0; break; }
            }
            if (!fl) puts("0");
            else lct.makeroot(1), lct.access(u), printf("%d\n", lct.imag[u] + sz[u]), mask ^= (lct.imag[u] + sz[u]);
        }
    }
    return 0;
}
//昔别君未婚,儿女忽成行。
//    -- 杜甫《赠卫八处士》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值