BZOJ 1014 JSOI2008 火星人prefix Splay+Hash+二分

來源:互聯網
上載者:User

標籤:bzoj   bzoj1014   splay   hash   二分   

題目大意:給定一個字串,提供下列操作:

1.查詢從x開始的尾碼和從y開始的尾碼的最長公用前置長度

2.將x位置的字元修改為y

3.在x位置的字元後面插入字元y

看到這題一開始我先懵住了。。。這啥。。我第一時間想到的是尾碼資料結構 但是不會寫 而且尾碼資料結構也不支援修改操作

後來無奈找了題解才知道是Hash+二分。。。 太強大了 Hash+二分打爆一切啊

用Splay維護這個字串的修改和插入操作 每個節點維護子串的Hash值 判斷時二分找到最長公用首碼

不過這道題還有一些注意事項

1.此題不卡Hash 不會出現ABBABAABBAABABBA之類噁心字串

2.題目沒有明說 但是字串上所有字元都是小寫字母

3.二分時注意上界的取值 不能超過字串長度 此題不保證x<y 一定要特判 否則就會RE

剩下就很裸了、、、80%達成 可以補個番了0.0

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 100100using namespace std;typedef unsigned long long ll;struct abcd{abcd *fa,*ls,*rs;int num,siz;ll hash;abcd(int x);void Push_Up();}*null=new abcd(0),*root=null;abcd :: abcd(int x){fa=ls=rs=null;num=x;siz=x?1:0;hash=x;}ll ksm[M];void abcd :: Push_Up(){siz=ls->siz+rs->siz+1;hash=ls->hash*ksm[rs->siz+1]+num*ksm[rs->siz]+rs->hash;}void Zig(abcd *x){abcd *y=x->fa;y->ls=x->rs;x->rs->fa=y;x->rs=y;x->fa=y->fa;if(y==y->fa->ls)y->fa->ls=x;else if(y==y->fa->rs)y->fa->rs=x;y->fa=x;y->Push_Up();if(y==root)root=x;}void Zag(abcd *x){abcd *y=x->fa;y->rs=x->ls;x->ls->fa=y;x->ls=y;x->fa=y->fa;if(y==y->fa->ls)y->fa->ls=x;else if(y==y->fa->rs)y->fa->rs=x;y->fa=x;y->Push_Up();if(y==root)root=x;}void Splay(abcd *x,abcd *Tar){while(1){abcd *y=x->fa,*z=y->fa;if(y==Tar)break;if(z==Tar){if(x==y->ls)Zig(x);elseZag(x);break;}if(x==y->ls){if(y==z->ls)Zig(y);Zig(x);}else{if(y==z->rs)Zag(y);Zag(x);}}x->Push_Up();}void Find(abcd *x,int y,abcd *z){while(1){if(y<=x->ls->siz)x=x->ls;else{y-=x->ls->siz;if(y==1)break;y--;x=x->rs;}}Splay(x,z);}char s[M];void Build_Tree(abcd *&x,int l,int r){if(l>r)return ;int mid=l+r>>1;x=new abcd(s[mid]-'a'+1);Build_Tree(x->ls,l,mid-1);Build_Tree(x->rs,mid+1,r);x->ls->fa=x;x->rs->fa=x;x->Push_Up();}void Initialize(){int i;ksm[0]=1;for(i=1;i<=100010;i++)ksm[i]=ksm[i-1]*31;scanf("%s",s);root=new abcd(19980402);root->rs=new abcd(19980402);Build_Tree(root->rs->ls,0,strlen(s)-1);root->rs->ls->fa=root->rs;root->rs->fa=root;root->rs->Push_Up();root->Push_Up();}bool Judge(int x,int y,int ans){Find(root,x,null);Find(root,x+ans+1,root);ll hash1=root->rs->ls->hash;Find(root,y,null);Find(root,y+ans+1,root);ll hash2=root->rs->ls->hash;return hash1==hash2;}int Query(int x,int y){int l=0,r=root->siz-2-max(x,y)+1;while(l+1<r){int mid=l+r>>1;if( Judge(x,y,mid) )l=mid;elser=mid;}if( Judge(x,y,r) )return r;return l;}int m;int main(){int i,x,y;char p[10];Initialize();cin>>m;for(i=1;i<=m;i++){scanf("%s",p);if(p[0]=='R'){scanf("%d%s",&x,p);Find(root,x+1,null);root->num=p[0]-'a'+1;root->Push_Up();}else if(p[0]=='I'){scanf("%d%s",&x,p);Find(root,x+1,null);Find(root,x+2,root);root->rs->ls=new abcd(p[0]-'a'+1);root->rs->ls->fa=root->rs;root->rs->Push_Up();root->Push_Up();}elsescanf("%d%d",&x,&y),printf("%d\n", Query(x,y) );}}



BZOJ 1014 JSOI2008 火星人prefix Splay+Hash+二分

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.