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. python基础-各模块文章导航

    python基础学习日志day5-各模块文章导航 python基础学习日志day5---模块使用 http://www.cnblogs.com/lixiang1013/p/6832475.html p ...

  2. android studio 解决avd启动问题 ----waiting for target device come online

    android studio 模拟器打不开,一直停留在第三方.waiting for target  device  come online 问题解决方法 方法1.Android Emulator 未 ...

  3. Filter过滤器-JavaWeb三大组件之一

    Servlet.Filter.Listener是JavaWeb的三大组件,给Web开发提供了很大的便利. 什么是Filter? Filter,过滤器.类似与生活中的净水器.空气净化器. JavaWeb ...

  4. Trie树子节点快速获取法

    今天做了一道leetcode上关于字典树的题:https://leetcode.com/problems/word-search-ii/#/description 一开始坚持不看别人的思路,完全自己写 ...

  5. web项目引入extjs小例子

    一个新的项目,前端用extjs实现!分享一下extjs开发的准备工作! 首先去下载extjs的资源包,这里我是随便在网上下载的! 打开之后 ,目录是这样的! 需要关注的几个文件夹: builds:压缩 ...

  6. 2016-2017-2 20155309南皓芯《java程序设计》第九周学习总结

    教材内容介绍 一 JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无须接触底层数据库驱动程序的差异性 JDBC标准分为两个部分:J ...

  7. PHP性能调优---php-fpm中启用慢日志配置(用于检测执行较慢的PHP脚本)

    虽然通过nginx accesslog可以记录用户访问某个接口或者网页所消耗的时间,但是不能清晰地追踪到具体哪个位置或者说函数慢,所以通过php-fpm慢日志,slowlog设置可以让我们很好的看见哪 ...

  8. 反向投影(BackProjection)

    如果一幅图像的区域中显示的是一种结构纹理或者一个独特的物体,那么这个区域的直方图可以看作一个概率函数,他给的是某个像素属于该纹理或物体的概率. 所谓反向投影就是首先计算某一特征的直方图模型,然后使用模 ...

  9. [水煮 ASP.NET Web API2 方法论](1-4)从 MVC Controller 链接到 API Controller 以及反向链接

    问题 想创建一个从 ASP.NET MVC controller 到 ASP.NET Web API controller 的直接链接,或者反向链接. 解决方案 可以使用 System.Web.Htt ...

  10. 插头DP学习笔记——从入门到……????

    我们今天来学习插头DP??? BZOJ 2595:[Wc2008]游览计划 Input 第一行有两个整数,N和 M,描述方块的数目. 接下来 N行, 每行有 M 个非负整数, 如果该整数为 0, 则该 ...