【AHOI2006】文本编辑器(splay tree)

【AHOI2006】文本编辑器

 

Time Limit:20000MS  Memory Limit:65536K
Total Submit:77 Accepted:47 
Case Time Limit:2000MS

Description

  这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器。你能帮助他吗? 
  为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 
  文本:由0个或多个字符构成的序列。这些字符的ASCII码在闭区间[32, 126]内,也就是说,这些字符均为可见字符或空格。 
  光标:在一段文本中用于指示位置的标记,可以位于文本的第一个字符之前,文本的最后一个字符之后或文本的某两个相邻字符之间。 
  文本编辑器:为一个可以对一段文本和该文本中的一个光标进行如下七条操作的程序。如果这段文本为空,我们就说这个文本编辑器是空的。 
   
任务:编写一个程序: 
  建立一个空的文本编辑器。 
  从输入文件中读入一些操作指令并执行。 
  对所有执行过的GET操作,将指定的内容写入输出文件。 

Input

  输入文件中第一行是指令条数N,以下是需要执行的N个操作。 
  除了回车符之外,输入文件的所有字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。 

Output

  输出文件editor.out的每行依次对应输入文件中每条GET指令的输出,不得有任何多余的字符。

Sample Input

10
Insert 13
Balanced eert
Move 2
Delete 5
Next
Insert 7
 editor
Move 0
Get
Move 11
Rotate 4
Get

Sample Output

B
t

Hint

  对输入数据我们有如下假定: 
  MOVE操作不超过50 000个,INSERT、DELETE和ROTATE操作作的总个数不超过6 000,GET操作不超过20 000个,PREV和NEXT操作的总个数不超过20 000。 
  所有INSERT插入的字符数之和不超过2M(1M=1 024*1 024)。 
  DELETE操作、ROTATE操作和GET操作执行时光标后必然有足够的字符。MOVE、PREV、NEXT操作不会把光标移动到非法位置。 
  输入文件没有错误。 

Source

xinyue

转载于:http://blog.csdn.net/ACM_cxlove/article/details/7803283

通过一个变量记录光标的位置即可。

MOVE:直接改变光标位置变量

INSERT:经典旋转,插入到根的右孩子的左子树

ROTATE:经典操作,区间反转,通过一个延迟标记记录,交换左右子树。同样要用到经典的旋转

GET:得到光标位置的后继,可以GET_KTH后然后GET_NEXT,也可以直接旋转,或者旋转后GET_MIN

PREV,NEXT:都只需要改变光标位置变量

在指定旋转的时候,我用的是GET_KTH,找到位置然后再Splay旋转

删除的时候把光标位置第pos节点旋转到root上,把要删除部分的下一个节点即第pos+k+1节点旋转到ch[pos][1]上,现在ch[ ch[pos][1]][0]的整颗子树都是要删除的部分了,pre[Key_value]=0;Key_value=0;这俩句就把整颗子树删掉了。。。so  easy!!!

代码当模版了:

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<algorithm>
#define N 1024*1024*2
#define Key_value ch[ch[root][1]][0]
using namespace std;
int n,q;
char key[N];
int size[N],pre[N],rev[N],pos;
int ch[N][2],tot,root,node[N];
//debug部分copy from hh
void Treaval(int x) {
    if(x) {
        Treaval(ch[x][0]);
        printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,key = %2c \n",x,ch[x][0],ch[x][1],pre[x],size[x],key[x]);
        Treaval(ch[x][1]);
    }
}
void debug() {printf("%d\n",root);Treaval(root);}
//以上Debug
void NewNode(int &r,char k,int father){
	r=++tot;
	ch[r][0]=ch[r][1]=0;
	pre[r]=father;
	rev[r]=0;
	key[r]=k;
}
void Push_Up(int r){
	size[r]=size[ch[r][0]]+size[ch[r][1]]+1;
}
void Push_Down(int r){
	if(rev[r]){
		swap(ch[r][0],ch[r][1]);
		rev[ch[r][0]]^=1;
		rev[ch[r][1]]^=1;
		rev[r]=0;
	}
}
void Bulid(int &r,int L,int R,int father,char *str){
	if(L>R)
		return ;
	int mid=(L+R)/2;
	NewNode(r,str[mid],father);
	Bulid(ch[r][0],L,mid-1,r,str);
	Bulid(ch[r][1],mid+1,R,r,str);
	Push_Up(r);
}
void Init(){
	tot=root=0;
	ch[root][0]=ch[root][1]=pre[root]=rev[root]=size[root]=0;
	Bulid(root,0,4,0,"     ");
}
void Rotate(int x,int kind){
	int y=pre[x];
	Push_Down(y);
	Push_Down(x);
	ch[y][!kind]=ch[x][kind];
	pre[ch[x][kind]]=y;
	if(pre[y])
		ch[pre[y]][ch[pre[y]][1]==y]=x;
	pre[x]=pre[y];
	ch[x][kind]=y;
	pre[y]=x;
	Push_Up(y);
}
void Splay(int r,int goal){
	Push_Down(r);
	while(pre[r]!=goal){
		if(pre[pre[r]]==goal)
			Rotate(r,ch[pre[r]][0]==r);
		else{
			int y=pre[r];
			int kind=(ch[pre[y]][0]==y);
			if(ch[y][kind]==r){
				Rotate(r,!kind);
				Rotate(r,kind);
			}
			else{
				Rotate(y,kind);
				Rotate(r,kind);
			}
		}
	}
	Push_Up(r);
	if(goal==0) root=r;
}
void RotateTo(int k,int goal) {
    int r=root;
    Push_Down(r);
    while(size[ch[r][0]]!=k){
        if(k<size[ch[r][0]]){
            r=ch[r][0];
        } else {
            k-=(size[ch[r][0]]+1);
            r=ch[r][1];
        }
        Push_Down(r);
    }
    Splay(r,goal);
}
int Get_Kth(int r,int k){
	Push_Down(r);
	int t=size[ch[r][0]]+1;
	if(t==k)
		return r;
	if(t>k)
		return Get_Kth(ch[r][0],k);
	else
		return Get_Kth(ch[r][1],k-t);
}
int Get_Min(int r){
	Push_Down(r);
	while(ch[r][0]){
		r=ch[r][0];
		Push_Down(r);
	}
	return r;
}
int Get_Max(int r){
	Push_Down(r);
	while(ch[r][1]){
		r=ch[r][1];
		Push_Down(r);
	}
	return r;
}
void Reversal(int k){
	int x=Get_Kth(root,pos);
	Splay(x,0);
	int y=Get_Kth(root,pos+k+1);
	Splay(y,root);
	rev[Key_value]^=1;
}
void Insert(char *str){
	int len=strlen(str);
	int x=Get_Kth(root,pos);
	Splay(x,0);
//	RotateTo(pos,0);
	int m=Get_Min(ch[root][1]);
	Splay(m,root);
	Bulid(Key_value,0,len-1,ch[root][1],str);
}
void Delete(int k){
//	RotateTo(pos,0);
	int x=Get_Kth(root,pos);
	Splay(x,0);
	int y=Get_Kth(root,pos+k+1);
	Splay(y,root);
	pre[Key_value]=0;
	Key_value=0;
	Push_Up(ch[root][1]);
	Push_Up(root);
}
char Get_Answer(){
	int x=Get_Kth(root,pos);
	Splay(x,0);
//	RotateTo(pos,0);
	int y=Get_Min(ch[root][1]);
	return key[y];
}
char str[N];
int main(){
	//freopen("editor1.in","r",stdin);
	while(scanf("%d",&q)!=EOF){
		Init();
		int k;
		pos=1;//光标初始位置
		while(q--){
			scanf("%s",str,&k);
			if(str[0]=='I'){
				scanf("%d",&k);
				getchar();
				gets(str);
				Insert(str);
			}
			else if(str[0]=='M'){
				scanf("%d",&k);
				pos=k+1;
			}
			else if(str[0]=='D'){
				scanf("%d",&k);
				Delete(k);
			}
			else if(str[0]=='R'){
				scanf("%d",&k);
				Reversal(k);
			}
			else if(str[0]=='G')
				printf("%c\n",Get_Answer());
			else if(str[0]=='P')
				pos--;
			else
				pos++;
		}

	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值