【平衡树维护序列+Hash求LCP】BZOJ1014(JSOI2008)[火星人prefix]题解

题目概述

给出一个字符串,给出m个操作,操作有这几种:1.替换一个字符。2.插入一个字符。3.求后缀x和后缀y的LCP。

解题报告

一看是变动的字符串,我们就想到了Splay,但是LCP怎么求?后缀数组肯定不行,因为一旦修改就要重新构造后缀数组。所以我们可以用基于Hash的LCP!因为Hash原先是递推构造的,所以在Splay中也可以方便维护Hash。

对于一个节点,Hash值递推关系如下(pw[i]表示Base^i,预先处理好,val表示当前节点权值):
Hash=son[0]->Hash*pw[1+son[1]->si]+val*pw[son[1]->si]+son[1]->Hash
将Hash视为一个Base进制数,则上述式子就不难理解了。

示例程序

#include<cstdio>
#include<algorithm>
using namespace std;
typedef unsigned long long ULL;
const int maxl=100000,Base=233333333;

int n,te;char s[maxl+5];ULL pw[maxl+5];
//==================================================
struct node
{
    node *son[2];int si;char val;ULL Ha;
    int cmp(int &k)
    {
        if (k<son[0]->si+1) return 0;
        if (k==son[0]->si+1) return -1;
        k-=son[0]->si+1;return 1;
    }
    void Pushup()
    {
        si=son[0]->si+1+son[1]->si;
        Ha=son[0]->Ha*pw[1+son[1]->si]+val*pw[son[1]->si]+son[1]->Ha;
        //递推Hash
    }
};
typedef node* P_node;
node tem[maxl+5];
P_node null=tem,len=null,ro=null;
P_node newnode(char ch)
{
    len++;len->son[0]=len->son[1]=null;
    len->si=1;len->val=ch;len->Ha=ch;
    return len;
}
P_node Build(int L,int R,char *s)
{
    if (L>R) return null;
    int mid=L+(R-L>>1);
    P_node now=newnode(s[mid]);
    now->son[0]=Build(L,mid-1,s);now->son[1]=Build(mid+1,R,s);
    now->Pushup();
    return now;
}
void Rotate(P_node &p,int d)
{
    P_node t=p->son[d^1];p->son[d^1]=t->son[d];t->son[d]=p;
    p->Pushup();t->Pushup();p=t;
}
void Splay(P_node &p,int k)
{
    int d1=p->cmp(k);
    if (d1!=-1)
    {
        P_node &p2=p->son[d1];int d2=p2->cmp(k);
        if (d2!=-1)
        {
            Splay(p2->son[d2],k);
            if (d1==d2) Rotate(p,d1^1),Rotate(p,d1^1); else
            Rotate(p2,d2^1),Rotate(p,d1^1);
        } else Rotate(p,d1^1);
    }
}
void getLR(P_node &p,int L,int R)
{
    L--;R++;
    Splay(p,L);p->cmp(R);
    Splay(p->son[1],R);
}
int Ask(P_node &p,int x,int y)
{
    int L=0,R=min(p->si-x,p->si-y);
    while (L<=R) //二分求LCP
    {
        int mid=L+(R-L>>1);ULL Hax,Hay;
        getLR(p,x,x+mid-1);Hax=p->son[1]->son[0]->Ha;
        getLR(p,y,y+mid-1);Hay=p->son[1]->son[0]->Ha;
        if (Hax==Hay) L=mid+1; else R=mid-1;
    }
    return R;
}
void Insert(P_node &p,int pos,P_node now)
{
    getLR(p,pos+1,pos);
    p->son[1]->son[0]=now;
    p->son[1]->Pushup();p->Pushup();
}
void Update(P_node &p,int pos,char ch)
{
    getLR(p,pos,pos);
    p->son[1]->son[0]->val=p->son[1]->son[0]->Ha=ch;
    p->son[1]->Pushup();p->Pushup();
}
//==================================================
bool Eoln(char ch) {return ch==10||ch==13||ch==EOF;}
int readi(int &x)
{
    int tot=0,f=1;char ch=getchar(),lst='+';
    while ('9'<ch||ch<'0') {if (ch==EOF) return EOF;lst=ch;ch=getchar();}
    if (lst=='-') f=-f;
    while ('0'<=ch&&ch<='9') tot=tot*10+ch-48,ch=getchar();
    x=tot*f;
    return Eoln(ch);
}
int reads(char *s)
{
    int len=0;char ch=getchar();if (ch==EOF) return EOF;
    s[++len]=ch;while (!Eoln(s[len])) s[++len]=getchar();s[len--]=0;
    return len;
}
char getrch() {char ch=getchar();while (ch!='Q'&&ch!='R'&&ch!='I') ch=getchar();return ch;}
void First_init()
{
    pw[0]=1;for (int i=1;i<=maxl;i++) pw[i]=pw[i-1]*Base;
    s[0]=s[n+1]=0;ro=Build(0,n+1,s);
}
void LNR(P_node p)
{
    if (p==null) return;
    LNR(p->son[0]);putchar(p->val);LNR(p->son[1]);
}
int main()
{
    freopen("program.in","r",stdin);
    freopen("program.out","w",stdout);
    n=reads(s);First_init();
    readi(te);
    while (te--)
    {
        char td=getrch();int x,y;readi(x);
        if (td=='Q') readi(y),printf("%d\n",Ask(ro,x+1,y+1)); else
        if (td=='R') Update(ro,x+1,getchar()); else
        Insert(ro,x+1,newnode(getchar()));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值