zoukankan      html  css  js  c++  java
  • HASH+平衡树 [JSOI2008]火星人prefix

    问题 I: [JSOI2008]火星人prefix
    时间限制: 2 Sec 内存限制: 162 MB
    题目描述
    火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。比方说,有这样一个字符串: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函数的值。
    输入
    第一行给出初始的字符串。第二行是一个非负整数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不超过当前字符串长度
    输出
    对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。
    样例输入
    madamimadam
    7
    Q 1 7
    Q 4 8
    Q 10 11
    R 3 a
    Q 1 7
    I 10 a
    Q 2 11
    样例输出
    5
    1
    0
    2
    1
    提示
    1、所有字符串自始至终都只有小写字母构成。
    2、M<=150,000
    3、字符串长度L自始至终都满足L<=100,000
    4、询问操作的个数不超过10,000个。
    对于第1,2个数据,字符串长度自始至终都不超过1,000
    对于第3,4,5个数据,没有插入操作。

    开始打了个挨个维护的HASH表。。。结果成功T..
    其实动态HASH是可以用平衡树维护的。我打的无旋treap(其实大部分人叫范浩强treap?)。
    主要问题在于父亲节点如何维护子树所表示区间的HASH。
    其实就是root->hash=lc->hash+root->v*p+rc->hash*p^(lc->size+1);
    因此区间的hash就很好维护了。
    只是查询的时候二分一下即可。

    #pragma GCC optimize("O3")
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define ll unsigned long long
    #define N 100005
    #define p 76543
    using namespace std;
    char s[N];int m,n;ll xp[N];
    namespace Treap
    {
        struct treap
        {
            treap* lc;treap* rc;
            int sz,id;ll hs,h;
            treap(){id=rand();lc=rc=NULL;h=sz=hs=0;}
            inline void updata()
            {
                sz=lc->sz+rc->sz+1;
                hs=lc->hs+h*xp[lc->sz]+rc->hs*xp[lc->sz+1];
            }
        }*null=new treap(),*root=null;
        typedef pair<treap*,treap*> hh;
        inline hh split(treap* f,int k)
        {
            if(f==null)return hh(null,null);hh y;
            if(f->lc->sz>=k)
                {y=split(f->lc,k),f->lc=y.second,f->updata(),y.second=f;}
            else
                {y=split(f->rc,k-f->lc->sz-1);f->rc=y.first;f->updata();y.first=f;}
            return y;
        }
        inline treap* merge(treap* a,treap* b)
        {
            if(a==null)return b;
            if(b==null)return a;
            if(a->id<b->id){a->rc=merge(a->rc,b);a->updata();return a;}
            else{b->lc=merge(a,b->lc);b->updata();return b;}
        }
        inline treap* newtreap(int x)
        {
            treap* o=new treap();
            o->lc=o->rc=null;
            o->sz=1;o->h=o->hs=x;
            return o;
        }
        inline void insert(int x,int k)
        {
            hh y=split(root,x);
            treap* f=newtreap(k);
            root=merge(merge(y.first,f),y.second);
            n++;
        }
        inline void change(int x,int k)
        {
            hh y=split(root,x-1);
            hh y1=split(y.second,1);
            y1.first->h=y1.first->hs=k;
            root=merge(y.first,merge(y1.first,y1.second));
        }
        inline ll get(int l,int len)
        {
            hh y=split(root,l-1);
            hh x=split(y.second,len);
            ll ans=x.first->hs;
            root=merge(y.first,merge(x.first,x.second));
            return ans;
        }
        inline int q(int a,int b)
        {
            int l=0,r=min(n-a+1,n-b+1),mid,ans;
            ll x1,x2;
            while(l<=r)
            {
                mid=(l+r)>>1;
                x1=get(a,mid),x2=get(b,mid);
                if(x1==x2)ans=mid,l=mid+1;
                else r=mid-1;
            }
            return ans;
        }
    }
    using namespace Treap; 
    void init()
    {
        scanf("%s",s+1);null->lc=null->rc=null;
        xp[0]=1;for(int i=1;i<=100000;i++)xp[i]=xp[i-1]*p;
        for(int i=1,len=strlen(s+1);i<=len;i++)insert(i-1,s[i]-'a');
    }
    void work()
    {
        scanf("%d",&m);int x,y;char ch[2];
        while(m--)
        {
            scanf("%s",ch);
            if(ch[0]=='Q')
                scanf("%d%d",&x,&y),printf("%d
    ",q(x,y));
            else
            {
                if(ch[0]=='R')
                    scanf("%d%s",&x,ch),change(x,ch[0]-'a');
                else
                    scanf("%d%s",&x,ch),insert(x,ch[0]-'a');
            }
        }
    }
    int main()
    {
        init();
        work();
    }
  • 相关阅读:
    Oracle TRCA 工具 说明
    Oracle TTS ORA39322: Cannot use transportable tablespace with different timezone version 说明
    Oracle 传输表空间(Transportable Tablespaces) 示例(二) 跨操作系统迁移表空间(endianness格式不同)
    易于在各手机平台移植的设计(转)
    Symbian 学习资源
    项目经理修炼手册
    ZIP 算法详解 (转!)
    google Android OS 学习资源、资料和笔记汇总(要看的)
    小白兔的求职遭遇
    数据结构和算法学习笔记(1)
  • 原文地址:https://www.cnblogs.com/QTY2001/p/7632649.html
Copyright © 2011-2022 走看看