BZOI 1507 [NOI2003] Editor
Background
After trying to solve problem EDIT1(Editor) and being ****ed by Brainf**k, Blue Mary decided to set another difficult problem about editor.
Description
Some definations:
- Text: It's a sequence that consists characters whose ASCII code is in [32,126].
- Cursor: It's a sign for pointing out the current position.It can be at the start or the end of the text or between two consecutive characters of the text.
Editor is a structure.It contains one text and one cursor.The operations are listed below:
--------------------------------------------------------------------------
| Name | Input format | function |
--------------------------------------------------------------------------
| Move(k) | Move k | Move the cursor after the kth character |
| | | in the text. If k=0, you should put |
| | | the cursor at the start of the text. |
--------------------------------------------------------------------------
| Insert(n,s) | Insert n s | Insert string s whose length is n(>=1) |
| | | after the cursor.The cursor doesn't move. |
--------------------------------------------------------------------------
| Delete(n) | Delete n | Delete n(>=1) characters after the cursor.|
| | | The cursor doesn't move. |
--------------------------------------------------------------------------
| Get(n) | Get n | Output n(>=1) characters after the cursor.|
--------------------------------------------------------------------------
| Prev() | Prev | Move the cursor one character forward. |
--------------------------------------------------------------------------
| Next() | Next | Move the cursor one character backward. |
--------------------------------------------------------------------------
If the text of a editor is empty,we say the editor is empty.
Here is an example._ denotes to the cursor,$ denotes to the start and the end.At start the editor is empty.
------------------------------------------------------------------------------
| Operation | Text after the operation | Output |
------------------------------------------------------------------------------
| INSERT(13,"Balanced tree") | $_Balanced tree$ | $$ |
------------------------------------------------------------------------------
| MOVE(2) | $Ba_lanced tree$ | $$ |
------------------------------------------------------------------------------
| DELETE(5) | $Ba_d tree$ | $$ |
------------------------------------------------------------------------------
| NEXT() | $Bad_ tree$ | $$ |
------------------------------------------------------------------------------
| INSERT(7," editor") | $Bad_ editor tree$ | $$ |
------------------------------------------------------------------------------
| MOVE(0) | $_Bad editor tree$ | $$ |
------------------------------------------------------------------------------
| GET(15) | $_Bad editor tree$ | $Bad editor tree$ |
------------------------------------------------------------------------------
Your task is:
- Build an empty editor.
- Read some operations from the standard input and operate them.
- For each Get operation, write the answer to the output.
Input
the very first line contains the number of testcases T(T<=4).T tests follow.
For each test, the first line is the number of operations N.N operations follow.
Blue Mary is so depressed with the problem EDIT1 that she decides to make the problem more difficult.So she inserts many extra line breaks in the string of the Insert operation.You must ignore them.
Except line breaks, all the charaters' ASCII code are in [32,126]. There's no extra space at the end of a line.
You can assume that for each test case:
- No invalid operation is in the input.
- Number of move operations is no more than 50000.
- Number of the total of insert and delete operations is no more than 4000.
- Number of the total of prev and next operations is no more than 200000.
- The characters inserted will not more than 2MB.The valid output will not more than 3MB.
Output
The output should contain T blocks corresponding to each testcase.
For each test case, the output should contain as many lines as the get operations in the input.Each line should contains the output of each get operation.
Example
Input: 1
15
Insert 26
abcdefghijklmnop
qrstuv wxy
Move 15
Delete 11
Move 5
Insert 1
^
Next
Insert 1
_
Next
Next
Insert 4
.\/.
Get 4
Prev
Insert 1
^
Move 0
Get 22 Output:
.\/.
abcde^_^f.\/.ghijklmno
Warning: large Input/Output data, be careful with certain languages
Blue Mary's note: the test case #1 has something wrong and it has been fixed on April 27th, 2007.Solutions has been rejudged. Please accept my apology.
题目取自SPOJ
几点注意的:
1、bzoj样例有误。
2、Insert操作如果读入长度用scanf("%d\n",&x)读,会自动过滤下一行空格,导致Wa90.
相信这是我写过最差的程序了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
using namespace std;
#define MAXN MAXT
#define MAXT 1024*1024*4+1000
int n,m;
struct Splay_tree
{
struct node
{
node *ch[],*fa;
char w;
int siz;
};
node E[MAXN],nil_node;
queue<node* > Q;
node *root,*nil;
Splay_tree()
{
int i;
for (i=;i<MAXT;i++)
{
Q.push(&E[i]);
}
nil_node.ch[]=nil_node.ch[]=NULL;
nil_node.w='#';
nil_node.siz=;
nil=&nil_node;
root=nil;
root->fa=nil;
}
void update(node *now)
{
if (now==nil)throw "illegal update";
now->siz=now->ch[]->siz+now->ch[]->siz+;
}
void rotate(node *now,int p)//注意不要改变nil的值,now的祖父节点或儿子节点可能为nil
{
node *pnt=now->fa;
now->fa=pnt->fa;
if (pnt->fa!=nil)
{
if (pnt->fa->ch[]==pnt)
{
pnt->fa->ch[]=now;
}else
{
pnt->fa->ch[]=now;
}
}
pnt->fa=now;
pnt->ch[p^]=now->ch[p];
if (now->ch[p]!=nil)now->ch[p]->fa=pnt;
now->ch[p]=pnt;
update(pnt);//注意顺序
update(now);
}
void splay(node *now,node *top)
{
node *pnt;
if (now==top)return ;
while (now->fa!=top)
{
pnt=now->fa;
if (pnt->ch[]==now)
{
if (pnt->fa!=top&&pnt->fa->ch[]==pnt)
{
rotate(pnt,);
}
rotate(now,);
}else
{
if (pnt->fa!=top&&pnt->fa->ch[]==pnt)
{
rotate(pnt,);
}
rotate(now,);
}
}
if (top==nil)
{
root=now;
}
}
node *get_node(int rank)
{
node *now=root;
if (now->siz<rank)throw "Not enough node";
while (true)
{
if (now->ch[]->siz+==rank)
{
return now;
}
if (now->ch[]->siz+<rank)
{
rank-=now->ch[]->siz+;
now=now->ch[];
}else
{
now=now->ch[];
}
}
return now;
}
node *get_min_node(node *now)
{
if (now==nil)throw "illegal call";
//if (now==nil)return nil;
while (now->ch[]!=nil)
{
now=now->ch[];
}
return now;
}
pair<node*,node*> split(int pos)
{
if (pos==)return make_pair(nil,root);
splay(get_node(pos),nil);
pair<node*,node*> ret;
ret.first=root;
ret.second=root->ch[];
root->ch[]->fa=nil;
root->ch[]=nil;
update(root);
root=NULL;
return ret;
}
node * merge(node * a1,node *a2)
{
if (a1==nil)return a2;
if (a2==nil)return a1;
root=a2;
splay(get_min_node(a2),nil);
root->ch[]=a1;
a1->fa=root;
update(root);
return root;
}
void insert(int pos,char ch)//插入ch后前面有pos个字符
{
node *now=Q.front();
Q.pop();
now->w=ch;
if (pos==)
{
if (root==nil)
{
now->fa=nil;
now->ch[]=now->ch[]=nil;
now->siz=;
root=now;
return ;
}
splay(get_min_node(root),nil);
now->fa=root;
root->ch[]=now;
now->ch[]=now->ch[]=nil;
update(now);
update(root);
return ;
}
splay(get_node(pos),nil);
if (root->ch[]!=nil)splay(get_min_node(root->ch[]),root);
now->fa=root;
now->ch[]=root->ch[];
root->ch[]->fa=now;
root->ch[]=now;
now->ch[]=nil;
update(now);
update(root);
splay(now,nil);
return ;
}
void insert2(int pos,char* str,int len)
{
if (len==)return ;
pair<node*,node*> pr1;
pr1=split(pos);
//scan(pr1.first);cout<<endl;
//scan(pr1.second);cout<<endl;
node *now=build_tree(str,,len-,nil);
root=merge(pr1.first,merge(now,pr1.second));
}
node *build_tree(char *str,int l,int r,node *fa)
{
if (l>r)return nil;
node *now=Q.front();
int mid=(l+r)/;
Q.pop();
now->fa=fa;
now->w=str[mid];
now->ch[]=build_tree(str,l,mid-,now);
now->ch[]=build_tree(str,mid+,r,now);
update(now);
return now;
}
void recycle(node *now)
{
if (now==nil)return ;
if (now->fa!=nil)
{
if (now==now->fa->ch[])
{
now->fa->ch[]=nil;
}else if (now==now->fa->ch[])
{
now->fa->ch[]=nil;
}
}
recycle(now->ch[]);
recycle(now->ch[]);
Q.push(now);
} void erase(int pos,int len)
{
pair<node*,node *> pr1,pr2;
pr1=split(pos);
root=pr1.second;
pr2=split(len);
recycle(pr2.first);
root=merge(pr1.first,pr2.second);
}
void scan(node *now)
{
if (now==nil)return;
if (now->siz!=now->ch[]->siz+now->ch[]->siz+)
{
throw "Size error";
}
if (now->ch[]!=nil&&now->ch[]->fa!=now)throw "Wrong ptr";
if (now->ch[]!=nil&&now->ch[]->fa!=now)throw "Wrong ptr";
scan(now->ch[]);
printf("%c",now->w);
scan(now->ch[]);
}
void scan2(node *now)
{
if (now==nil)return;
scan2(now->ch[]);
printf("%c",now->w);
scan2(now->ch[]);
}
void print_str(int pos,int len)
{
if (!len){puts("");return ;}/**/
if (pos==)
{
if (len==root->siz)
{
scan(root);
puts("");
return ;
}
splay(get_node(len+),nil);
scan(root->ch[]);
puts("");
return ;
}
splay(get_node(pos),nil);
if (pos+len<=root->siz)splay(get_node(pos+len),root);
node *temp=root->ch[],*temp2=root->ch[]->ch[];
root->ch[]=nil;root->ch[]->ch[]=nil;
scan2(root->ch[]);puts("");
root->ch[]=temp;root->ch[]->ch[]=temp2;
return ;
}
}spt;
int i;
char str1[MAXN];
int main()
{
//freopen("editor2.in","r",stdin);
//freopen("out2.txt","w",stdout);
try
{
int j,k,x,y;
scanf("%d",&n);
char od[];
int m,p;
char *ptr;
char ch;
int nowad=;
int root2;
for (i=;i<n;i++)
{
scanf("%s ",od);
switch (od[])
{ case 'M': scanf("%d\n",&x);
nowad=x;
break;
case 'I':
scanf("%d",&x);
getchar();
for (j=;j<x;j++)
{
ch=getchar();
if (ch<||ch>)
{
j--;
continue;
}
str1[j]=ch;
//if (i!=1)cerr<<ch<<endl;;
//spt.insert(nowad+j,ch);
}
str1[x]='\0';
spt.insert2(nowad,str1,x);
if (x)ch=getchar();
break;
case 'D':
scanf("%d\n",&x);
spt.erase(nowad,x);
break;
case'G':
scanf("%d\n",&x);
spt.print_str(nowad,x);
break;
case'P':
nowad--;
break;
case'N':
nowad++;
break;
}
/* cout<<od<<" "<<x<<endl;
if (od[0]=='I')cout<<str1<<endl;
cout<<"<<";spt.scan(spt.root);cout<<"["<<nowad<<"]";
cout<<endl;*/
}
}catch (const char* err)
{
cout<<err;
return ;
}
return ;
}
BZOI 1507 [NOI2003] Editor的更多相关文章
- 1507: [NOI2003]Editor(块状链表)
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4157 Solved: 1677[Submit][Stat ...
- 1507: [NOI2003]Editor
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MB Submit: 3535 Solved: 1435 [Submit][St ...
- 【BZOJ】1507: [NOI2003]Editor(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1507 当练splay模板了,发现wjmzbmr的splay写得异常简介,学习了.orzzzzzzzz ...
- BZOJ 1507 [NOI2003]Editor
Description Input 输 入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉 ...
- BZOJ 1507 NOI2003 Editor Splay
题目大意: 1.将光标移动到某一位置 2.在光标后插入一段字符串 3.删除光标后的一段字符 4.输出光标后的一段字符 5.光标-- 6.光标++ 和1269非常像的一道题,只是弱多了 几个问题须要注意 ...
- BZOJ1507 [NOI2003]Editor 【splay】
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MB Submit: 4129 Solved: 1660 [Submit][St ...
- BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor
BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...
- 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay
[bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...
- BZOJ1507 [NOI2003]Editor
是一道裸的Splay(反正我不会Splay,快嘲笑我!) 只需要维护在数列上加点删点操作即可,不会写Splay的渣渣只好Orz iwtwiioi大神一下了.(后来发现程序直接抄来了...) 就当我的第 ...
随机推荐
- mongodb 学习笔记 09 -- shard分片
概述 shard 分片 就是 把不同的数据分在不同的server 模型 当中: 用户对mongodb的操作都是向mongs请求的 configsvr 用于保存,某条数据保存在哪个sha ...
- android79 Fragment生命周期
切换成01时依次调用onCreate,onStart,onResume方法,切换到03的时候01依次onPause,onStop,onDestroy,03依次onCreate,onStart,onRe ...
- ubuntu权限管理常用命令 分类: linux ubuntu 学习笔记 2015-07-05 14:15 77人阅读 评论(0) 收藏
1.chmod 第一种方式 chomd [{ugoa}{+-=}{rwx}] [文件或者目录] u 代表该文件所属用户 g 代表该文件所属用户组 o 代表访客 a 代表所有用户 +-=分别表示增加权限 ...
- CentOS7上GitHub/GitLab多帐号管理SSH Key
由于公司团队使用 GitLab 来托管代码,同时,个人在 Github 上还有一些代码仓库,可公司邮箱与个人邮箱是不同的,由此产生的 SSH key 也是不同的,这就造成了冲突 ,文章提供此类问题的解 ...
- 简单地使用jquery的validate
简单地使用jquery的validate ——@梁WP 摘要:本文通过一个很简单的例子,讲解了jquery validate的最基础使用方式. 一.源代码 注意事项都写在代码的注释里了,哈哈. < ...
- not exists 跟not in 纪念一下
- c#静态成员和静态类
说起静态类,你可能会联想到实例类.这两者并不难区分,前者(静态类)只在内存中创建一个,而后者(实例类)则是每次实例化后,就会再内存创建一份.今天来简单聊一下静态类的理解. 代码情景: class Pr ...
- 创建Java线程池
线程池的作用: 线程池作用就是限制系统中执行线程的数量. 根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果:少了浪费了系统资源,多了造成系统拥挤效率不高.用线程池控制线程数量,其他线 ...
- 利用查询提示优化SQL
数据库环境:SQL SERVER 2005 我们来看一下SQL语句及对应的数据量 SELECT COUNT(*) FROM cinvout_02 a WHERE ( a.dept_id IN ( SE ...
- 三步走起 提升 iOS 审核通过率 下篇
根据2015年的数据统计情况,并结合<苹果应用商店审核指南>,互娱 iOS 预审组通过细分将预审工作划为3大模块:客户端资源检查.应用内容检查和提审资源检查. 在上一篇文章中,Bugly ...