【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay
【BZOJ1269】[AHOI2006]文本编辑器editor
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操作不会把光标移动到非法位置。 输入文件没有错误。
题解:依旧是Splay裸题,但是有一点很坑:
就是gets()在读入整行时会忽略前导的空格,于是我们必须在读入上一个变量时加一个scanf("%*c")或者getchar()来防止将空格省略掉,或者一个一个字符读进来
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n,m,root,now,tot;
struct node
{int ch[2],fa,v,siz,tag;
}s[1024*2048+10];
char str[1024*2048+10];
void pushup(int x)
{s[x].siz=s[s[x].ch[0]].siz+s[s[x].ch[1]].siz+1;
}
void pushdown(int x)
{if(s[x].tag){swap(s[x].ch[0],s[x].ch[1]);s[s[x].ch[0]].tag^=1,s[s[x].ch[1]].tag^=1;s[x].tag=0;}
}
int find(int y,int x)
{if(!x) return 0;pushdown(x);if(s[s[x].ch[0]].siz>=y) return find(y,s[x].ch[0]);if(s[s[x].ch[0]].siz+1==y) return x;return find(y-s[s[x].ch[0]].siz-1,s[x].ch[1]);
}
void rotate(int x,int &k)
{int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);if(z) s[z].ch[(y==s[z].ch[1])]=x;if(y==k) k=x;s[x].fa=z,s[y].fa=x,s[y].ch[d]=s[x].ch[d^1];if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;s[x].ch[d^1]=y;pushup(y),pushup(x);
}
void splay(int x,int &k)
{while(x!=k){int y=s[x].fa,z=s[y].fa;if(y!=k){if((x==s[y].ch[0])^(y==s[z].ch[0])) rotate(x,k);else rotate(y,k);}rotate(x,k);}
}
void build(int l,int r,int last,int d)
{if(l>r) return ;int tmp=++tot,mid=l+r>>1;s[tmp].fa=last;s[last].ch[d]=tmp;s[tmp].v=str[mid]-32;build(l,mid-1,tmp,0),build(mid+1,r,tmp,1);pushup(tmp);
}
int main()
{scanf("%d",&n);s[1].ch[1]=2,s[1].siz=2,s[2].siz=1,s[2].fa=1,now=root=1,tot=2;for(int i=1;i<=n;i++){scanf("%s",str);switch(str[0]){case 'M':{scanf("%d",&now),now++;break;}case 'I':{scanf("%d%*c",&m);gets(str);splay(find(now,root),root),splay(find(now+1,root),s[root].ch[1]);build(0,m-1,s[root].ch[1],0);pushup(s[root].ch[1]),pushup(root);break;}case 'D':{scanf("%d",&m);splay(find(now,root),root),splay(find(now+m+1,root),s[root].ch[1]);s[s[root].ch[1]].ch[0]=0;pushup(s[root].ch[1]),pushup(root);break;}case 'R':{scanf("%d",&m);splay(find(now,root),root),splay(find(now+m+1,root),s[root].ch[1]);s[s[s[root].ch[1]].ch[0]].tag^=1;break;}case 'G':{printf("%c\n",s[find(now+1,root)].v+32);break;}case 'P':{now--;break;}case 'N':{now++;break;}}}return 0;
}
【BZOJ1507】[NOI2003]editor
Description
Input
输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作。其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉它们(如果难以理解这句话,可以参考样例)。 除了回车符之外,输入文件的所有字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。 这里我们有如下假定: MOVE操作不超过50000个,INSERT和DELETE操作的总个数不超过4000,PREV和NEXT操作的总个数不超过200000。 所有INSERT插入的字符数之和不超过2M(1M=1024*1024),正确的输出文件长度不超过3M字节。 DELETE操作和GET操作执行时光标后必然有足够的字符。MOVE、PREV、NEXT操作必然不会试图把光标移动到非法位置。 输入文件没有错误。 对C++选手的提示:经测试,最大的测试数据使用fstream进行输入有可能会比使用stdio慢约1秒。
Output
输出文件editor.out的每行依次对应输入文件中每条GET指令的输出。
Sample Input
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
Sample Output
abcde^_^f.\/.ghijklmno
题解:本题注意事项:
1.读入有换行,要一个一个字符读入
2.网上有人说要防止溢出,感觉没啥用
3.输出时候不要太麻烦哦~
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n,m,root,now,tot;
struct node
{int ch[2],fa,v,siz;
}s[1024*2048+10];
char str[1024*2048+10];
void pushup(int x)
{s[x].siz=s[s[x].ch[0]].siz+s[s[x].ch[1]].siz+1;
}
int find(int y,int x)
{if(!x) return 0;if(s[s[x].ch[0]].siz>=y) return find(y,s[x].ch[0]);if(s[s[x].ch[0]].siz+1==y) return x;return find(y-s[s[x].ch[0]].siz-1,s[x].ch[1]);
}
void rotate(int x,int &k)
{int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);if(z) s[z].ch[(y==s[z].ch[1])]=x;if(y==k) k=x;s[x].fa=z,s[y].fa=x,s[y].ch[d]=s[x].ch[d^1];if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;s[x].ch[d^1]=y;pushup(y),pushup(x);
}
void splay(int x,int &k)
{while(x!=k){int y=s[x].fa,z=s[y].fa;if(y!=k){if((x==s[y].ch[0])^(y==s[z].ch[0])) rotate(x,k);else rotate(y,k);}rotate(x,k);}
}
void build(int l,int r,int last,int d)
{if(l>r) return ;int tmp=++tot,mid=l+r>>1;s[tmp].fa=last,s[last].ch[d]=tmp,s[tmp].v=str[mid]-32;build(l,mid-1,tmp,0),build(mid+1,r,tmp,1);pushup(tmp);
}
void print(int x)
{if(s[x].ch[0]) print(s[x].ch[0]);putchar(s[x].v+32);if(s[x].ch[1]) print(s[x].ch[1]);
}
int main()
{scanf("%d",&n);int i,j;s[1].ch[1]=2,s[1].siz=2,s[2].siz=1,s[2].fa=1,now=root=1,tot=2;for(i=1;i<=n;i++){scanf("%s",str);switch(str[0]){case 'M':{scanf("%d",&now),now++; break;}case 'I':{scanf("%d%*c",&m);for(j=0;j<m;j+=(str[j]!='\n'&&str[j]!='\r')) str[j]=getchar();splay(find(now,root),root),splay(find(now+1,root),s[root].ch[1]);build(0,m-1,s[root].ch[1],0);pushup(s[root].ch[1]),pushup(root);break;}case 'D':{scanf("%d",&m);splay(find(now,root),root),splay(find(now+m+1,root),s[root].ch[1]);s[s[root].ch[1]].ch[0]=0;pushup(s[root].ch[1]),pushup(root);break;}case 'G':{scanf("%d",&m);splay(find(now,root),root),splay(find(now+m+1,root),s[root].ch[1]);print(s[s[root].ch[1]].ch[0]);printf("\n");break;}case 'P':{now--; break;}case 'N':{now++; break;}}}return 0;
}
转载于:https://www.cnblogs.com/CQzhangyu/p/6497999.html
【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay相关推荐
- BZOJ 1269: [AHOI2006]文本编辑器editor Splay
F.A.Qs Web Board Home ProblemSet Status Ranklist Contest ModifyUser yejinru(0) Logout 捐赠本站 Notice: ...
- 1269: [AHOI2006]文本编辑器editor
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4718 Solved: 1807 [Sub ...
- Bzoj1269 [AHOI2006]文本编辑器editor
Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3678 Solved: 1380 Description 这些日子,可可不和卡卡一起玩了,原来可可 ...
- 小程序 html编辑器,小程序富文本编辑器editor初体验
终于,微信在5月9号的v2.7.0版本中新增了 editor富文本编辑器组件,今天有时间了准备体验一下 在5月6日的时候写了一篇小程序富文本解析的「伪需求」,从wxParse到towxml的坑,当时还 ...
- 小程序(二十五)微信小程序富文本编辑器editor上传图片
官方给出的示例代码中图片上传功能是将图片上传至小程序的缓存中,代码如下所示: insertImage(e) {console.log(e);const that = thiswx.chooseImag ...
- layui获取select 文本_小程序富文本编辑器editor初体验
终于,微信在5月9号的v2.7.0版本中新增了 editor富文本编辑器组件,今天有时间了准备体验一下 在5月6日的时候写了一篇小程序富文本解析的「伪需求」,从wxParse到towxml的坑,当时还 ...
- 小程序开发笔记(二):微信小程序富文本编辑器editor的使用
小程序在去年5月的 v2.7.0 版本新增了组件editor富文本编辑器,但对于像我这种开发新手,要熟练使用还是有一定难度.所以记录一下我的学习过程,希望对大家有帮助. 小程序有详细的微信开发 ...
- 博客系统-写文章之富文本编辑器editor
editor 直接再官网下载下来 之后从下载下来的文件选取有用的文件放到项目专门包下,注意下图 可以选择下载好的文件中的上图部分,放入新建md中 然后就是写文章页面 <!DOCTYPE html ...
- 微信小程序富文本编辑器editor初体验-图片上传
https://developers.weixin.qq.com/miniprogram/dev/component/editor.html 以前没有小程序富文本编辑器,只能输入文字,图片上传后,在服 ...
- 微信小程序富文本编辑器editor+rich-text(附源码)
之前小程序一直都是使用自己写的接口,小程序远程获取使用富文本编辑器的内容,比如UEditor,wangEditor等.我们可以选择插件wxparse和自带的rich-text,但是这些并不能完全转义他 ...
最新文章
- 网络营销之CPA、CPS、CPM、CPT、CPC 是什么
- [HNOI2008 GT考试]
- OCR 深度学习 综述
- Java基础篇:抽象类与接口
- easy-ui表单校验---针对单个字段,多重校验(有参数校验+无参数检验)
- OOo on ready---VB篇
- keil之编辑环境配置
- Running Trinity in multiple steps
- Script标签解决跨域
- 消息队列MQ如何保证消息的幂等性
- android 评分条 RatingBar 使用及自定义
- 问题:anaconda 中 tensorflow 与tensorflow-gpu 在tf.image.resize_images()上的区别
- c语言棋类ai怎么写搜索,新手立体四子棋AI教程(4)——启发式搜索与主程序
- 计算机系统中设置保护系统还原,win7系统电脑设置系统还原点的操作方法
- 全民编程新时代—TPYBoard重磅产品TurnipBit来了!
- ai星际探索 爪子_通过特征空间探索实现可解释的人工智能
- append、appendTo、appendChild、prepend
- 得之坦然,失之淡然,顺其自然,争其必然。真的太经典了啊!
- MySQL5.7修改了my.ini文件后服务启动不了
- DSP6678 RapidIO基本原理之一