BZOJ2258 文本校对 (Splay Hash 二分答案)

题目大意

同bzoj1014,注意这里面的询问很鬼畜。要注意二分答案的上界的设定。


题解

题解见bzoj1014:

读完题目我首先想到的是后缀数据结构,但是后缀数据结构并不支持修改操作。

想了一会儿发现思路并不太对于是我就去翻了大爷的题解,发现是splay+hash+二分答案。大体思路就是用splay结构维护这个字符串,并且在每一个结点维护子树字符串的哈希值。

由于询问操作的答案具有单调性,所以可以用二分答案解决。二分最大公共前缀的长度,检验两部分的哈希值是否相等即可。

一开始听说这题卡哈希,不过貌似并没有啊…
但是这题用unsigned long long过不去要用unsigned int才不会TLE啊!


代码

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

typedef unsigned int ull;
const int p=8823823;
ull powp[int(1e5)+111];
void init_p() {
    powp[0]=1;
    for(int i=1;i<int(1e5)+111;i++) powp[i]=powp[i-1]*p;
    return;
}

const int maxn=int(2e5)+111;
int n,m;
char s[maxn];
int a[maxn];

struct Node {
    int key,siz;
    ull val;
    Node *ch[2],*fa;
    Node();
    Node(int);
    int son() {
        if(fa->ch[1]==this) return 1;
        else if(fa->ch[0]==this) return 0;
        return -1;
    }
    inline int cmp(int pos) {
        if(pos==ch[0]->siz+1) return -1;
        return pos<ch[0]->siz+1?0:1;
    }
    inline void maintain() {
        siz=ch[0]->siz+1+ch[1]->siz;
        val=ch[0]->val*powp[ch[1]->siz+1] + key*powp[ch[1]->siz] + ch[1]->val;
        return;
    }
}*null=new Node(),*root=null,*orgpos[50005];

Node :: Node():key(0) {
    siz=null?1:0;
    val=key;
    ch[0]=ch[1]=fa=null;
}
Node :: Node(int _key):key(_key) {
    siz=null?1:0;
    val=key;
    ch[0]=ch[1]=fa=null;
}

void print(Node* cur) {
    if(cur==null) return;
    print(cur->ch[0]);
    cout<<char('a'+cur->key-1);
    print(cur->ch[1]);
}

#define mid ((l+r)>>1)
void build(Node* &cur,int l,int r) {
    if(l>r) {cur=null; return;}
    cur=new Node(a[mid]);
    orgpos[mid]=cur;
    build(cur->ch[0],l,mid-1), cur->ch[0]->fa=cur;
    build(cur->ch[1],mid+1,r), cur->ch[1]->fa=cur;
    cur->maintain();
}
#undef mid

inline void Rotate(Node* cur,int dir) {
    Node *tmp=cur->ch[dir^1]; int dir2;
    cur->ch[dir^1]=tmp->ch[dir]; tmp->ch[dir]->fa=cur;
    tmp->ch[dir]=cur;
    cur->maintain(), tmp->maintain();
    if(~(dir2=cur->son())) cur->fa->ch[dir2]=tmp;
    tmp->fa=cur->fa, cur->fa=tmp; //cur=tmp
    return;
}

inline void Splay(Node* cur) {
    int dir;
    while(~(dir=cur->son())) {
        if(dir==cur->fa->son()) Rotate(cur->fa->fa,dir^1);
        Rotate(cur->fa,dir^1);
    }
}
inline Node* _find(Node* cur,int k) {
    int dir=cur->cmp(k);
    if(dir==1) k-=cur->ch[0]->siz+1;
    if(dir==-1) return cur;
    else return _find(cur->ch[dir],k);
}

inline Node* Merge(Node* lhs,Node* rhs) {
    if(lhs==null) return rhs;
    if(rhs==null) return lhs;
    Node *tmp=_find(lhs,lhs->siz);
    Splay(tmp);
    tmp->ch[1]=rhs, rhs->fa=tmp, tmp->maintain();
    return tmp;
}

inline void Split(Node* org,int dir,Node*& l,Node*& r) {
    Splay(org);
    if(!dir) l=org->ch[0], r=org;
    else l=org, r=org->ch[1];
    org->ch[dir]->fa=null, org->ch[dir]=null, org->maintain();
    return ;
}

void Modify(Node* &cur,int pos,int val) {
    Node *tmp=_find(cur,pos);
    Splay(tmp);
    tmp->key=val, tmp->maintain();
    cur=tmp;
}

inline void Insert(Node* &cur,int pos,int val) {
    Node *add=new Node(val);
    if(pos==0) {
        cur=Merge(add,cur);
        return;
    }
    if(pos>cur->siz) pos=cur->siz;
    Node *lhs,*rhs;
    Split(_find(cur,pos),1,lhs,rhs);
    lhs=Merge(Merge(lhs,add),rhs);
    cur=lhs;
}

inline unsigned long long Get_hash(Node* cur,int pos,int l,int len) {
    Node *lhs,*mid,*rhs,*tmp;
    ull res;
    Split(orgpos[pos],0,lhs,mid);
    Split(_find(mid,len),1,mid,rhs);
    res=mid->val;
    root=Merge(Merge(lhs,mid),rhs);
    return res;
}

inline int solve(int ox,int oy,int x,int y) {
    int l=1, r=root->siz-max(x,y)+1, res=0;
    while(l<=r) {
        int mid=(l+r)>>1;
        if(Get_hash(root,ox,x,mid)==Get_hash(root,oy,y,mid)) l=mid+1, res=mid;
        else r=mid-1;
    }
    return res;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("check18.in","r",stdin);
    freopen("output.txt","w",stdout);
#endif // ONLINE_JUDGE
    init_p();
    scanf("%s%d",s+1,&m);

    n=strlen(s+1);
    for(int i=1;i<=n;i++) a[i]=s[i]-'a'+1;
    build(root,1,n);

    for(int i=0;i<m;i++) {
        char op[5];
        scanf("%s%*c",op);
        if(op[0]=='Q') {
            int x,y,xx,yy;
            scanf("%d%d",&x,&y);
            Splay(orgpos[x]), xx=orgpos[x]->ch[0]->siz+1;
            Splay(orgpos[y]), yy=orgpos[y]->ch[0]->siz+1;
            Splay(root);
            printf("%d\n",solve(x,y,xx,yy));
        } else if(op[0]=='I') {
            int pos; char ch;
            scanf("%c %d",&ch,&pos);
            Insert(root,pos-1,ch-'a'+1);
        }
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值