Poj 2887-Big String Splay
Posted 微弱的世界
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Poj 2887-Big String Splay相关的知识,希望对你有一定的参考价值。
Big String
Description You are given a string and supposed to do some string manipulations. Input The first line of the input contains the initial string. You can assume that it is non-empty and its length does not exceed 1,000,000. The second line contains the number of manipulation commands N (0 < N ≤ 2,000). The following N lines describe a command each. The commands are in one of the two formats below:
All characters in the input are digits or lowercase letters of the English alphabet. Output For each Q command output one line containing only the single character queried. Sample Input ab 7 Q 1 I c 2 I d 4 I e 2 Q 5 I f 1 Q 3 Sample Output a d e Source POJ Monthly--2006.07.30, zhucheng
|
题意:给一个字符串,要求查询某一位的字母,或在某一位前插入一个字母.
题解:
Splay的单点插入,和单点查询.
记得内存大小要把操作的2000加上。
速度422ms,还不错。。。
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 #define MAXN 1002010 9 struct node 10 { 11 int left,right,size; 12 char val; 13 }tree[MAXN]; 14 int father[MAXN]; 15 char str[MAXN]; 16 void Pushup(int k) 17 { 18 tree[k].size=tree[tree[k].left].size+tree[tree[k].right].size+1; 19 } 20 void rotate(int x,int &root) 21 { 22 int y=father[x],z=father[y]; 23 if(y==root)root=x; 24 else 25 { 26 if(tree[z].left==y)tree[z].left=x; 27 else tree[z].right=x; 28 } 29 if(tree[y].left==x) 30 { 31 father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y; 32 } 33 else 34 { 35 father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y; 36 } 37 Pushup(y);Pushup(x); 38 } 39 void Splay(int x,int &root) 40 { 41 while(x!=root) 42 { 43 int y=father[x],z=father[y]; 44 if(y!=root) 45 { 46 if((tree[y].left==x)^(tree[z].left==y))rotate(x,root); 47 else rotate(y,root); 48 } 49 rotate(x,root); 50 } 51 } 52 void Build(int l,int r,int f) 53 { 54 if(l>r)return; 55 int now=l,fa=f; 56 if(l==r) 57 { 58 tree[now].val=str[l];tree[now].size=1; 59 father[now]=fa; 60 if(l<f)tree[fa].left=now; 61 else tree[fa].right=now; 62 return; 63 } 64 int mid=(l+r)/2; 65 now=mid; 66 Build(l,mid-1,mid);Build(mid+1,r,mid); 67 tree[now].val=str[mid];father[now]=fa; 68 Pushup(now); 69 if(mid<f)tree[fa].left=now; 70 else tree[fa].right=now; 71 } 72 int Find(int root,int rank) 73 { 74 if(rank==tree[tree[root].left].size+1)return root; 75 if(rank<=tree[tree[root].left].size)return Find(tree[root].left,rank); 76 else return Find(tree[root].right,rank-tree[tree[root].left].size-1); 77 } 78 int main() 79 { 80 int m,i,k,L,R,x,y,z,lstr,rt,n; 81 char ch,fh[2]; 82 scanf("%s",str+2); 83 lstr=strlen(str+2); 84 m=lstr+2; 85 Build(1,m,0); 86 rt=(1+m)/2; 87 scanf("%d",&n); 88 for(i=1;i<=n;i++) 89 { 90 scanf("\n%s",fh); 91 if(fh[0]==‘Q‘) 92 { 93 scanf("%d",&k); 94 L=k;R=k+2; 95 x=Find(rt,L);y=Find(rt,R); 96 Splay(x,rt);Splay(y,tree[x].right); 97 z=tree[y].left; 98 printf("%c\n",tree[z].val); 99 } 100 else 101 { 102 scanf("\n%c %d",&ch,&k); 103 L=k;R=k+1; 104 x=Find(rt,L);y=Find(rt,R); 105 Splay(x,rt);Splay(y,tree[x].right); 106 tree[y].left=++m; 107 z=tree[y].left;tree[z].size=1; 108 father[z]=y;tree[z].val=ch; 109 Pushup(y);Pushup(x); 110 } 111 } 112 return 0; 113 }
以上是关于Poj 2887-Big String Splay的主要内容,如果未能解决你的问题,请参考以下文章