• Bzoj2555: SubString


    题面

    传送门

    Sol

    考虑求每个串在模板串中出现的次数
    就在(sam)上走就行了,因为它的每一条路径都是它的一个子串
    走到最后一个点,若匹配,那么它的答案就是(parent)树的这个点的子树大小

    然后带修改就写个(LCT)维护(parent)树就好了
    (LCT)维护子树信息,非常好写

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
     
    IL int Input(){
        RG int x = 0, z = 1; RG char c = getchar();
        for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
        return x * z;
    }
     
    const int maxn(1200005);
     
    struct LCT{
        int fa[maxn], val[maxn], sum[maxn], ch[2][maxn];
         
        IL int Son(RG int x){
            return ch[1][fa[x]] == x;
        }
         
        IL int Isroot(RG int x){
            return ch[0][fa[x]] != x && ch[1][fa[x]] != x;
        }
         
        IL void Update(RG int x){
            sum[x] = val[x] + sum[ch[0][x]] + sum[ch[1][x]];
        }
         
        IL void Rotate(RG int x){
            RG int y = fa[x], z = fa[y], c = Son(x);
            if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z;
            ch[c][y] = ch[!c][x], fa[ch[c][y]] = y;
            ch[!c][x] = y, fa[y] = x;
            Update(y);
        }
         
        IL void Splay(RG int x){
            for(RG int y = fa[x]; !Isroot(x); Rotate(x), y = fa[x])
                if(!Isroot(y)) Son(x) ^ Son(y) ? Rotate(x) : Rotate(y);
            Update(x);
        }
         
        IL void Access(RG int x){
            for(RG int y = 0; x; y = x, x = fa[x]){
                Splay(x);
                val[x] += sum[ch[1][x]] - sum[y];
                ch[1][x] = y, Update(x);
            }
        }
         
        IL void Cut(RG int x){
            Access(x), Splay(x);
            ch[0][x] = fa[ch[0][x]] = 0, Update(x);
        }
         
        IL void Link(RG int x, RG int y){
            Splay(x), Access(y), Splay(y);
            fa[x] = y, val[y] += sum[x], Update(y);
        }
    } lct;
     
    int tot = 1, last = 1, trans[26][maxn], fa[maxn], len[maxn];
     
    IL void Extend(RG int c){
        RG int p = last, np = ++tot;
        last = np, len[np] = len[p] + 1, lct.val[np] = 1;
        while(p && !trans[c][p]) trans[c][p] = np, p = fa[p];
        if(!p) fa[np] = 1, lct.Link(np, 1);
        else{
            RG int q = trans[c][p];
            if(len[q] == len[p] + 1) fa[np] = q, lct.Link(np, q);
            else{
                RG int nq =  ++tot;
                lct.Link(nq, fa[q]);
                fa[nq] = fa[q], len[nq] = len[p] + 1;
                for(RG int i = 0; i < 26; ++i) trans[i][nq] = trans[i][q];
                if(fa[q]) lct.Cut(q);
                lct.Link(q, nq), lct.Link(np, nq);
                fa[q] = fa[np] = nq;
                while(p && trans[c][p] == q) trans[c][p] = nq, p = fa[p];
            }
        }
    }
     
    int q, mask, ans;
    char s[maxn * 3], op[8];
     
    IL void NewOption(RG int l){
            RG int seed = mask;
        for(RG int i = 0; i < l; ++i){
                seed = (seed * 131 + i) % l;
            swap(s[i], s[seed]);
        }
    }
     
    int main(RG int argc, RG char *argv[]){
        q = Input(), scanf(" %s", s);
        for(RG int i = 0, l = strlen(s); i < l; ++i) Extend(s[i] - 'A');
        for(RG int i = 1; i <= q; ++i){
            scanf(" %s %s", op, s);
            RG int l = strlen(s);
            NewOption(l);
            if(op[0] == 'Q'){
                RG int nw = 1;
                for(RG int i = 0; i < l; ++i) nw = trans[s[i] - 'A'][nw];
                if(!nw) ans = 0;
                else lct.Access(nw), lct.Splay(nw), ans = lct.val[nw];
                printf("%d
    ", ans), mask ^= ans;
            }
            else for(RG int i = 0; i < l; ++i) Extend(s[i] - 'A');
        }
        return 0;
    }
    
    
  • 相关阅读:
    Chapter 03—Getting Started with graphs
    设置颜色的函数:rgb,hsv,color palettes
    plot()与dev 函数族的使用
    attach()与detach()的使用
    Chapter 02—Creating a dataset(Part3-补充材料Stat/Transfer)
    Chapter 02—Creating a dataset(Part2)
    Chapter 02—Creating a dataset(Part1)
    网络七层协议的通俗理解
    使用Git将本地项目上传到码云
    如何在Idea中使用Git将项目代码上传到码云
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8921251.html
Copyright © 2020-2023  润新知