javascript
[JSOI2008 Prefix火星人]
[關鍵字]:splay hash 二分
[題目大意]:給出一個字符串,求出給定的兩個后綴的的最長公共前綴。在求的過程中會有改變或在某個位置添加字符的操作。
//============================================================================================
[分析]:一聽最長公共前綴馬上想到后綴數組,但因為是動態維護所以后綴數組也無能為力??梢园炎址胂蟪梢粋€數組,于是變成了動態插入和改變一個序列,還要能快速找到兩個子區間——splay。求最長公共前綴可以用二分答案加驗證的方法,二分長度先提取出這個區間然后判斷這個區間的根節點的hash值(實際就是這棵子樹的hash)是否和另一個相同。hash值得判斷可以用進制法:hash=ord(s[1])*270+ord(s[2])*271+……+ord(s[n])*27n-1,在旋轉時更新hash值為:hash[lc]+s[v]*27^(size[lc]+1)+hash[rc]*27^(size[lc]+2)。插入操作和一般splay插入無異修改只要修改掉這個點的值和hash值就行了。
[代碼]:
View Code #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std;const int MAXL=100015; const int MOD=9875321;struct node {node *c[2],*f;int dat,size,hash; }*root,SC[MAXL],*null; char s[MAXL]; int m,SS; int d[MAXL];void Debug(node *v) {if (v==null) return;Debug(v->c[0]);printf("%d ",v->dat);Debug(v->c[1]); }node *New(int d,int h,node *fa) {node *e=SC+ ++SS;e->size=1,e->f=fa;e->c[1]=e->c[0]=null;e->dat=d,e->hash=h;return e; }void Update(node *v) {if (v==null) return;v->size=v->c[0]->size+v->c[1]->size+1;v->hash=(long long)(v->c[0]->hash+(long long)v->dat*d[(v->c[0]->size+1)]+(long long)v->c[1]->hash*d[(v->c[0]->size+2)])%MOD; }void Prepare() {SS=-1;null=NULL;null=New(0,0,NULL);null->size=0;root=New(0,0,null);root->c[1]=New(0,0,root);d[1]=1;for (int i=2;i<=100010;++i)d[i]=(d[i-1]*27)%MOD;Update(root); }void Rotate(node *x,int o) {node *y=x->f;y->c[o]=x->c[!o];y->c[o]->f=y;x->f=y->f;if (y->f->c[0]==y)y->f->c[0]=x;elsey->f->c[1]=x;x->c[!o]=y;y->f=x;if (y==root) root=x;Update(y); }void Splay(node *x,node *fa) {while (x->f!=fa)if (x->f->f==fa)if (x->f->c[0]==x)Rotate(x,0);elseRotate(x,1);elseif (x->f->f->c[0]==x->f)if (x->f->c[0]==x)Rotate(x->f,0),Rotate(x,0);elseRotate(x,1),Rotate(x,0);else if (x->f->c[1]==x)Rotate(x->f,1),Rotate(x,1);elseRotate(x,0),Rotate(x,1);Update(x); }void Select(int k,node *fa) {node *t=root;while (1){if (k==t->c[0]->size+1) break;if (k<t->c[0]->size+1) t=t->c[0];if (k>t->c[0]->size+1) k-=t->c[0]->size+1,t=t->c[1];}Splay(t,fa); }int Hash(int x,int y) {Select(x,null),Select(y+2,root);return root->c[1]->c[0]->hash; }void Ask(int x,int y) {int ans=0,n=root->size-2;for (int i=1<<18;i;i>>=1)if (x+i-1<=n && y+i-1<=n && Hash(x,x+i-1)==Hash(y,y+i-1))ans+=i,x+=i,y+=i;printf("%d\n",ans); }void Ins(int x,char ch) {Select(x+1,null),Select(x+2,root);root->c[1]->c[0]=New(ch-'a',ch-'a',root->c[1]);Splay(root->c[1]->c[0],null);//Debug(root),printf("\n"); }void Rep(int x,char ch) {Select(x,null),Select(x+2,root);node *v=root->c[1]->c[0];v->dat=ch-'a';v->hash=(long long)(v->c[0]->hash+(long long)v->dat*d[(v->c[0]->size+1)]+(long long)v->c[1]->hash*d[(v->c[0]->size+2)])%MOD;//Debug(root),printf("\n"); }void Init() {node *z,*t;Prepare();scanf("%s",s);int n=strlen(s);z=t=New(s[0]-'a',s[0]-'a',null);for (int i=1;i<n;++i)z=z->c[1]=New(s[i]-'a',s[i]-'a',z);root->c[1]->c[0]=t,t->f=root->c[1];Splay(z,null);//Debug(root),printf("\n"); }void Solve() {char ch; int x,y;scanf("%d",&m),scanf("%c",&ch);for (int i=1;i<=m;++i){scanf("%c",&ch);if (ch=='Q') scanf("%d %d",&x,&y),Ask(x,y); elseif (ch=='I') scanf("%d %c",&x,&ch),Ins(x,ch); elsescanf("%d %c",&x,&ch),Rep(x,ch);scanf("%c",&ch);} }int main() {freopen("in","r",stdin);freopen("out","w",stdout);Init();Solve();return 0; }轉載于:https://www.cnblogs.com/procedure2012/archive/2012/04/13/2445062.html
總結
以上是生活随笔為你收集整理的[JSOI2008 Prefix火星人]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 定时器定时更新
- 下一篇: 消息称14代酷睿3nm GPU跳票 台积