1269: [AHOI2006]文本编辑器editor

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1213  Solved: 454
[Submit][Status]

Description

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

Input

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

Output

依次对应输入文件中每条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

 /* ***********************************************
Author :kuangbin
Created Time :2013/8/26 22:47:15
File Name :F:\2013ACM练习\专题学习\splay_tree_2\文本编辑器editor.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; /*
* 对字符串进行插入、删除、反转、查询第k个字符等操作
* Move k : 将光标移到到第k个字符之后
* Insert n S : 在光标后插入长度为n的字符串S,光标位置不变
* Delete n :删除光标后的n个字符,光标位置保持不变
* Rotate n :反转光标后的n个字符,光标位置不变
* Get :输出光标后的一个字符
* Prev :光标前移一个字符
* Next :光标后移一个字符
*
*
*用一个变量记录光标位置,对于Move,Prev,Next 直接改变这个变量
*
*/
#define Key_value ch[ch[root][1]][0]
const int MAXN = **+;
int ch[MAXN][],pre[MAXN],rev[MAXN],size[MAXN];
int root,tot1;
char key[MAXN];
int s[MAXN],tot2; int pos;//光标位置
char str[MAXN];//需要插入的字符串
void NewNode(int &r,int father,char k)
{
if(tot2) r = s[tot2--];
else r = ++tot1;
ch[r][] = ch[r][] = ;
pre[r] = father;
rev[r] = ;
key[r] = k;
size[r] = ;
}
void Update_Rev(int r)
{
if(!r)return;
swap(ch[r][],ch[r][]);
rev[r] ^= ;
}
void push_up(int r)
{
size[r] = size[ch[r][]] + size[ch[r][]] + ;
}
void push_down(int r)
{
if(rev[r])
{
Update_Rev(ch[r][]);
Update_Rev(ch[r][]);
rev[r] = ;
}
}
void Build(int &x,int l,int r,int father)
{
if(l > r)return;
int mid = (l+r)/;
NewNode(x,father,str[mid]);
Build(ch[x][],l,mid-,x);
Build(ch[x][],mid+,r,x);
push_up(x);
}
void Init()
{
pos = ;
root = tot1 = tot2 = ;
ch[root][] = ch[root][] = pre[root] = size[root] = rev[root] = ;
NewNode(root,,' ');
NewNode(ch[root][],root,' ');
push_up(ch[root][]);
push_up(root);
}
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]][]==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)
{
push_down(pre[r]);
push_down(r);
Rotate(r,ch[pre[r]][]==r);
}
else
{
push_down(pre[pre[r]]);
push_down(pre[r]);
push_down(r);
int y = pre[r];
int kind = ch[pre[y]][]==y;
if(ch[y][kind] == r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
push_up(r);
if(goal == )root = r;
}
int Get_kth(int r,int k)
{
push_down(r);
int t = size[ch[r][]] + ;
if(t == k)return r;
if(t > k)return Get_kth(ch[r][],k);
else return Get_kth(ch[r][],k-t);
}
//在光标后插入长度为len的字符串
void INSERT(int len)
{
Splay(Get_kth(root,pos+),);
Splay(Get_kth(root,pos+),root);
Build(Key_value,,len-,ch[root][]);
push_up(ch[root][]);
push_up(root);
}
void erase(int r)
{
if(r)
{
s[++tot2] = r;
erase(ch[r][]);
erase(ch[r][]);
}
}
void DELETE(int len)
{
Splay(Get_kth(root,pos+),);
Splay(Get_kth(root,pos+len+),root);
erase(Key_value);
pre[Key_value] = ;
Key_value = ;
push_up(ch[root][]);
push_up(root);
}
void Reverse(int len)
{
Splay(Get_kth(root,pos+),);
Splay(Get_kth(root,pos+len+),root);
Update_Rev(Key_value);
push_up(ch[root][]);
push_up(root);
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
int x;
char op[];
while(scanf("%d",&n) == )
{
Init();
while(n--)
{
scanf("%s",&op);
if(op[] == 'M')
{
scanf("%d",&x);
pos = x;
}
else if(op[] == 'P')pos--;
else if(op[] == 'N')pos++;
else if(op[] == 'I')
{
scanf("%d%*c",&x);
gets(str);
INSERT(x);
}
else if(op[] == 'D')
{
scanf("%d",&x);
DELETE(x);
}
else if(op[] == 'R')
{
scanf("%d",&x);
Reverse(x);
}
else if(op[] == 'G')
{
printf("%c\n",key[Get_kth(root,pos+)]);
}
}
} return ;
}

BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)的更多相关文章

  1. BZOJ 1269: [AHOI2006]文本编辑器editor( splay )

    splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...

  2. bzoj 1269 [AHOI2006]文本编辑器editor

    原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 伸展树的运用,如下: #include<cstdio> #include ...

  3. 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay

    [BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...

  4. 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay

    [bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...

  5. 【BZOJ】1269: [AHOI2006]文本编辑器editor(Splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1269 这题RE2次啊,好不爽啊,我一直以为是splay的问题,其实是数组开小了......(我老犯这 ...

  6. [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:   文本:由0个或 ...

  7. [BZOJ1269] [AHOI2006] 文本编辑器editor (splay)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:  文本:由0个或多 ...

  8. 1269: [AHOI2006]文本编辑器editor

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5269  Solved: 2037[Submit][Status][Discuss] Descript ...

  9. BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4633  Solved: 1782 [Sub ...

随机推荐

  1. 【前端vue开发】vue开发watch检测的使用

    <span style="color:#006600;"><div id="app"> <input type="tex ...

  2. python基础--subprocess模块

    可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen*          --废弃 popen2.*           --废弃 commands.* ...

  3. css文字环绕图片--遇到的问题及解决方法

    一.前言 需要实现一个文字环绕图片的效果,心想so easy嘛. 1)代码部分 <style> .img-left { border: 3px solid #005588; width:3 ...

  4. 原生js封装dom操作库

    var utils = (function(window) { var flag = "getComputedStyle" in window; function win(attr ...

  5. 汇编看C函数调用

    http://blog.csdn.net/wishfly/article/details/5022008   简单的函数调用,通过简单的函数调用反汇编可以清楚了解如下 1.栈到底是什么,如何操纵栈的? ...

  6. 数据库中INFORMATION_SCHEMA的说明及使用

    第一个查询看看库里有多少个表,表名等select * from INFORMATION_SCHEMA.TABLES information_schema这张数据表保存了MySQL服务器所有数据库的信息 ...

  7. Effective STL 笔记 -- Item 9: Choose carefully among erasing options

    假设有一个容器中存放着 int ,Container<int> c, 现在想从其中删除数值 1963,可以有如下方法: 1: c.erase(remove(c.begin(), c.end ...

  8. 【HackerRank】How Many Substrings?

    https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...

  9. linux kernel.shmall shemax shemin解释

        Linux X86-64操作系统,Oracle 10g数据库,由8G加到16G,把kernel.shmmax参数改到17179869184(16G)后,发现只要修改sga_max_size和s ...

  10. pygame模块参数汇总(python游戏编程)

    一.HelloWorld pygame.init() #初始函数,使用pygame的第一步: pygame.display.set_mod((600,500),0,32) #生成主屏幕screen:第 ...