zoukankan      html  css  js  c++  java
  • BZOJ1014:[JSOI2008]火星人prefix——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014

    Description

      火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。比方说,有这样一个字符串:madamimadam,
    我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m 现在,
    火星人定义了一个函数LCQ(x, y),表示:该字符串中第x个字符开始的字串,与该字符串中第y个字符开始的字串
    ,两个字串的公共前缀的长度。比方说,LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0 在研究LCQ函数的过程
    中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出LCQ函数的值;同样,
    如果求出了LCQ函数的值,也可以很快地将该字符串的后缀排好序。 尽管火星人聪明地找到了求取LCQ函数的快速
    算法,但不甘心认输的地球人又给火星人出了个难题:在求取LCQ函数的同时,还可以改变字符串本身。具体地说
    ,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此
    复杂的问题中,火星人是否还能够做到很快地求取LCQ函数的值。

    Input

      第一行给出初始的字符串。第二行是一个非负整数M,表示操作的个数。接下来的M行,每行描述一个操作。操
    作有3种,如下所示
    1、询问。语法:Qxy,x,y均为正整数。功能:计算LCQ(x,y)限制:1<=x,y<=当前字符串长度。
    2、修改。语法:Rxd,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字
    符串长度。
    3、插入:语法:Ixd,x是非负整数,d是字符。功能:在字符串第x个字符之后插入字符d,如果x=0,则在字
    符串开头插入。限制:x不超过当前字符串长度

    Output

      对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。

    Sample Input

    madamimadam
    7
    Q 1 7
    Q 4 8
    Q 10 11
    R 3 a
    Q 1 7
    I 10 a
    Q 2 11

    Sample Output

    5
    1
    0
    2
    1

    ——————————————————————————————

    很难的平衡树题目,稍有不注意就会TLE收场。

    参考(终于不用写抄袭了):https://www.cnblogs.com/ljh2000-jump/p/5974875.html

    将操作按照难度来划分慢慢讲。

    1.修改操作:

    将要修改的点转到根节点修改即可避免大量update操作节省时间。

    2.插入操作:

    将插入位置转到根节点,再将插入位置的后一位转到根节点儿子上,这样我们只需要在插入位置的后一位的左儿子加上结点即可,只需要两个update节省时间。

    3.查询操作:

    首先二分答案,关键问题是check函数怎么写。

    首先思考我们字符串的比较方法——哈希。

    (貌似取模会变慢,所以选择自然溢出哈希)

    然后思考我们能否将一整个字符串全部转到一个子树上去——完全可以。

    (将字符串头-1转到根,字符串尾+1转到根的儿子,则后者的左儿子所在子树就是我们要求的)

    (所以在最开始我们需要为字符串提供开头结尾字符)

    最后将他们使劲联想到一起。

    我们开一个ha[i]数组表示以i为根的子树,将它展成字符串之后字符串的哈希值为多少。

    这并不是很难维护,所以不讲了,看代码的update部分就差不多了。

    于是我们做完了这道题。

    (3个小时……)

    #include<cstdio>
    #include<queue>
    #include<cctype>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    typedef unsigned long long ll;
    const int N=100010;
    inline int read(){
        int X=0,w=0;char ch=0;
        while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
        while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
        return w?-X:X;
    }
    inline char getc(){
        char ch=getchar();
        while(ch==' '||ch=='
    ')ch=getchar();
        return ch;
    }
    int fa[N],tr[N][2],size[N];
    ll key[N],bit[N],ha[N];
    int root,sz,n,m;
    char s[N];
    inline bool get(int x){
        return tr[fa[x]][1]==x;
    }
    inline void update(int x){
        size[x]=1+size[tr[x][0]]+size[tr[x][1]];
        ha[x]=ha[tr[x][1]];
        ha[x]+=key[x]*bit[size[tr[x][1]]];
        ha[x]+=ha[tr[x][0]]*bit[size[tr[x][1]]+1];
        return;
    }
    inline void rotate(int x){
        int old=fa[x],oldf=fa[old],which=get(x);
        tr[old][which]=tr[x][which^1];tr[x][which^1]=old;
        fa[tr[old][which]]=old;fa[old]=x;fa[x]=oldf;
        if(oldf)tr[oldf][tr[oldf][1]==old]=x;
        update(old);update(x);
        return;
    }
    inline void splay(int x,int y){
        int f=fa[x];
        while(f!=y){
            if(fa[f]!=y)rotate((get(x)==get(f)?f:x));
            rotate(x);f=fa[x];
        }
        if(!y)root=x;
        return;
    }
    inline int findx(int x){
        int now=root;
        while(233){
            if(tr[now][0]&&x<=size[tr[now][0]])now=tr[now][0];
            else{
                int temp=(tr[now][0]?size[tr[now][0]]:0)+1;
                if(x<=temp)return now;
                x-=temp;now=tr[now][1];
            }
        }
    }
    inline void insert(int x,char ch){
        sz++;tr[sz][0]=tr[sz][1]=fa[sz]=0;
        key[sz]=ha[sz]=ch-'a'+1;size[sz]=1;
        if(!root){root=sz;return;}
        splay(findx(x),0);
        if(x+1>size[root]){
            tr[root][1]=sz;fa[sz]=root;
            update(root);
            return;
        }
        splay(findx(x+1),root);
        int now=tr[root][1];
        tr[now][0]=sz;fa[sz]=now;
        update(now);update(root);
        return;
    }
    inline void change(int x,char ch){
        splay(findx(x),0);
        key[root]=ch-'a'+1;
        update(root);
        return;
    }
    inline bool check(int l,int x,int y){
        splay(findx(x-1),0);
        splay(findx(x+l),root);
        ll ha1=ha[tr[tr[root][1]][0]];
        splay(findx(y-1),0);
        splay(findx(y+l),root);
        ll ha2=ha[tr[tr[root][1]][0]];
        if(ha1==ha2)return 1;
        return 0;
    }
    inline int query(int x,int y){
        int ans=0;
        int l=1,r=size[root]-max(x,y),mid;
        while(l<=r){
            int mid=(l+r)>>1;
            if(check(mid,x,y)){
                l=mid+1;
                ans=mid;
            }else r=mid-1;
        }
        return ans;
    }
    int main(){
        cin>>s+1;
        n=strlen(s+1);
        m=read();
        bit[0]=1;
        for(int i=1;i<N;i++)bit[i]=bit[i-1]*27;
        insert(0,'~');
        for(int i=1;i<=n;i++)insert(i,s[i]);
        insert(n+1,'~');
        for(int i=1;i<=m;i++){
            char ch=getc();
            int x=read()+1;
            if(ch=='Q')printf("%d
    ",query(x,read()+1));
            if(ch=='R')change(x,getc());
            if(ch=='I')insert(x,getc());
        }
        return 0;
    }
  • 相关阅读:
    hdu 1203 I NEED A OFFER! 01背包
    hdu2602 Bone Collector 01背包
    hdu 2546 饭卡 01背包
    ACM-ICPC 2018 焦作赛区网络预赛 G Give Candies
    ACM-ICPC 2018 焦作赛区网络预赛 I Save the Room
    poj1564 Sum It Up dfs水题
    VS2019生成并使用动态链接库(自测有用)
    英语发音基础五天搞定之第三天
    Some thoughts in the Novel Coronavirus holiday
    ​英语发音基础五天搞定之第二天
  • 原文地址:https://www.cnblogs.com/luyouqi233/p/8150381.html
Copyright © 2011-2022 走看看