BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)
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
Insert 13
Balanced eert
Move 2
Delete 5
Next
Insert 7
editor
Move 0
Get
Move 11
Rotate 4
Get
Sample Output
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)的更多相关文章
- BZOJ 1269: [AHOI2006]文本编辑器editor( splay )
splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...
- bzoj 1269 [AHOI2006]文本编辑器editor
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 伸展树的运用,如下: #include<cstdio> #include ...
- 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay
[BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...
- 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay
[bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...
- 【BZOJ】1269: [AHOI2006]文本编辑器editor(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1269 这题RE2次啊,好不爽啊,我一直以为是splay的问题,其实是数组开小了......(我老犯这 ...
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
- [BZOJ1269] [AHOI2006] 文本编辑器editor (splay)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或多 ...
- 1269: [AHOI2006]文本编辑器editor
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5269 Solved: 2037[Submit][Status][Discuss] Descript ...
- BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4633 Solved: 1782 [Sub ...
随机推荐
- MyBatis3.4.0以上的分页插件错误:Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.stateme
错误: Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named p ...
- 命令行执行Django脚本
命令行执行Django脚本 - #效仿manage.py加入的环境变量在脚本的文件加入 - #手动注册django所有的APP import sys,os ---------------------- ...
- CF529B 【Group Photo 2 (online mirror version)】
贪心枚举最后方案中最大的h,设为maxh若某个人i的wi与hi均大于maxh,则此方案不可行若某个人恰有一个属性大于maxh,则可确定他是否换属性剩下的人按wi-hi从大到小排序后贪心选择O(nlog ...
- SVN入门教程总结
参考: SVN使用笔记 SVN入门必备教程 一看就懂 SVN使用教程总结 版本控制器:SVN教程 菜鸟教程之SVN教程 极客学院之SVN教程 SVN(SubVersion)简介: 为什么要使用SVN( ...
- 使用jsplumb的一些笔记
欢迎就是需要使用jsplumb跟正在使用jsplumb的一起讨论 欢迎私聊 1.关于jsplumb的connection的一些事件 ####connection拖动的事件 instance.bind( ...
- TypeScript 2 : 获取当前日期及前后范围日期【Array】
前言 今天有个接口字段需求,要写一个今天及前几天的日期传过去: 在网上找了下都木有什么比较好的方案:就自己写了一个. 因为技术栈就是NG2+TS2+WEBPACK,这里的代码需要一定的TS2及ES6的 ...
- Executor
一.为什么需要Executor?为了更好的控制多线程,JDK提供了一套线程框架Executor,帮助开发人员有效的进行线程控制.他们都在java.util.concurrent包中,是JDK并发包的核 ...
- mvn本地库导入jar包
导入脚本 #!/bin/sh mvn deploy:deploy-file -DgroupId=com.xxx.xxx -DartifactId=包名 -Dversion=4.0 -Dpackag ...
- appium----新版appium 1.11.1 支持ByName定位
org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this sess ...
- JS几种变量交换方式以及性能分析对比
前言 "两个变量之间的值得交换",这是一个经典的话题,现在也有了很多的成熟解决方案,本文主要是列举几种常用的方案,进行大量计算并分析对比. 起由 最近做某个项目时,其中有一个需求是 ...