NOI2003 文本编辑器editor
1507: [NOI2003]Editor
Time Limit: 5 Sec Memory Limit: 162 MB
Submit: 1908 Solved: 738
[Submit][Status]
Description
Input
Output
Sample Input
Insert 26
abcdefghijklmnop
qrstuv wxy
Move 16
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
HINT
Source
题解:
我靠。。。
刚开始WA,后来栈溢出,然后TLE,还让不让人活了。。。
代码:
1.递归,中序遍历输出结果
{$inline on}
{$M 10000000,0,maxlongint}
const maxn=+;
var s,fa:array[..maxn] of longint;
v:array[..maxn] of char;
c:array[..maxn,..] of longint;
i,j,n,m,x,y,z,tot,rt,now,l,r:longint;
ch:char;
st,st2:ansistring;
procedure pushup(x:longint);inline;
begin
s[x]:=s[c[x,]]+s[c[x,]]+;
end;
procedure rotate(x:longint;var k:longint);inline;
var y,z,l,r:longint;
begin
y:=fa[x];z:=fa[y];
l:=ord(c[y,]=x);r:=l xor ;
if y=k then k:=x else c[z,ord(c[z,]=y)]:=x;
fa[x]:=z;fa[y]:=x;fa[c[x,r]]:=y;
c[y,l]:=c[x,r];c[x,r]:=y;
pushup(y);pushup(x);
end;
procedure splay(x:longint;var k:longint);inline;
var y,z:longint;
begin
while x<>k do
begin
y:=fa[x];z:=fa[y];
if y<>k then
if (c[z,]=y) xor (c[y,]=x) then rotate(x,k)
else rotate(y,k);
rotate(x,k);
end;
end;
function find(x,rank:longint):longint;inline;
var l,r:longint;
begin
l:=c[x,];r:=c[x,];
if s[l]+=rank then exit(x)
else if s[l]>=rank then exit(find(l,rank))
else exit(find(r,rank-s[l]-));
end;
procedure print(x:longint);inline;
begin
if x= then exit;
print(c[x,]);
write(v[x]);
print(c[x,]);
end; procedure main;
begin
readln(n);
l:=maxn-+;r:=maxn-+;
rt:=l;
c[l,]:=r;fa[r]:=l;s[l]:=;s[r]:=;
now:=;tot:=;
for i:= to n do
begin
read(ch);
case ch of
'P':begin
readln;
dec(now);
end;
'N':begin
readln;
inc(now);
end;
'M':begin
while ch<>' ' do read(ch);readln(x);
now:=x+;
end;
'I':begin
while ch<>' ' do read(ch);readln(m);
readln(st);while length(st)<m do begin readln(st2);st:=st+st2;end;
for j:= to m do begin inc(tot);fa[tot]:=tot-;v[tot]:=st[j];s[tot]:=m-j+;c[tot,]:=;if j<>m then c[tot,]:=tot+ else c[tot,]:=;end;
x:=find(rt,now);y:=find(rt,now+);
splay(x,rt);splay(y,c[x,]);
c[y,]:=tot-m+;fa[tot-m+]:=y;
pushup(y);pushup(x);
end;
'D':begin
while ch<>' ' do read(ch);readln(m);
x:=find(rt,now);y:=find(rt,now+m+);
splay(x,rt);splay(y,c[x,]);
z:=c[y,];fa[z]:=;c[y,]:=;
pushup(y);pushup(x);
end;
'G':begin
while ch<>' ' do read(ch);readln(m);
x:=find(rt,now);y:=find(rt,now+m+);
splay(x,rt);splay(y,c[x,]);
print(c[y,]);writeln;
end;
end;
end;
end;
begin
assign(input,'input.txt');assign(output,'output.txt');
reset(input);rewrite(output);
main;
close(input);close(output);
end.
2.人工栈
{$inline on}
{$M 1000000000,0,maxlongint}
const maxn=+;
var s,fa:array[..maxn] of longint;
v:array[..maxn] of char;
c:array[..maxn,..] of longint;
i,j,n,m,x,y,z,tot,rt,now,l,r,top:longint;
ch:char;
sta:array[..maxn*] of record a,b:longint;end;
st,st2:ansistring;
function min(x,y:longint):longint;inline;
begin
if x<y then exit(x) else exit(y);
end;
procedure pushup(x:longint);inline;
begin
s[x]:=s[c[x,]]+s[c[x,]]+;
end;
procedure rotate(x:longint;var k:longint);inline;
var y,z,l,r:longint;
begin
y:=fa[x];z:=fa[y];
l:=ord(c[y,]=x);r:=l xor ;
if y=k then k:=x else c[z,ord(c[z,]=y)]:=x;
fa[x]:=z;fa[y]:=x;fa[c[x,r]]:=y;
c[y,l]:=c[x,r];c[x,r]:=y;
pushup(y);pushup(x);
end;
procedure splay(x:longint;var k:longint);inline;
var y,z:longint;
begin
while x<>k do
begin
y:=fa[x];z:=fa[y];
if y<>k then
if (c[z,]=y) xor (c[y,]=x) then rotate(x,k)
else rotate(y,k);
rotate(x,k);
end;
end;
procedure print(x:longint);
var i,j:longint;
begin
top:=;sta[top].a:=x;sta[top].b:=;
while top> do
begin
i:=sta[top].a;j:=sta[top].b;dec(top);
if j= then write(v[i])
else
begin
if c[i,]> then
begin
inc(top);sta[top].a:=c[i,];sta[top].b:=;
end;
inc(top);sta[top].a:=i;sta[top].b:=;
if c[i,]> then
begin
inc(top);sta[top].a:=c[i,];sta[top].b:=;
end;
end;
end;
end; function find(x,rank:longint):longint;inline;
var l,r:longint;
begin
l:=c[x,];r:=c[x,];
if s[l]+=rank then exit(x)
else if s[l]>=rank then exit(find(l,rank))
else exit(find(r,rank-s[l]-));
end;
procedure main;
begin
readln(n);
l:=maxn-+;r:=maxn-+;
rt:=l;
c[l,]:=r;fa[r]:=l;s[l]:=;s[r]:=;
now:=;tot:=;
for i:= to n do
begin
read(ch);
case ch of
'P':begin
readln;
dec(now);
end;
'N':begin
readln;
inc(now);
end;
'M':begin
while ch<>' ' do read(ch);readln(x);
now:=x+;
end;
'I':begin
while ch<>' ' do read(ch);readln(m);
readln(st);while length(st)<m do begin readln(st2);st:=st+st2;end;
for j:= to m do begin inc(tot);fa[tot]:=tot-;v[tot]:=st[j];s[tot]:=m-j+;c[tot,]:=;if j<>m then c[tot,]:=tot+ else c[tot,]:=;end;
x:=find(rt,now);y:=find(rt,now+);
splay(x,rt);splay(y,c[x,]);
c[y,]:=tot-m+;fa[tot-m+]:=y;
pushup(y);pushup(x);
end;
'D':begin
while ch<>' ' do read(ch);readln(m);
x:=find(rt,now);y:=find(rt,min(tot+,now+m+));
splay(x,rt);splay(y,c[x,]);
z:=c[y,];fa[z]:=;c[y,]:=;
pushup(y);pushup(x);
end;
'G':begin
while ch<>' ' do read(ch);readln(m);
x:=find(rt,now);y:=find(rt,min(tot+,now+m+));
splay(x,rt);splay(y,c[x,]);
print(c[y,]);writeln;
end;
end;
end;
end;
begin
assign(input,'input.txt');assign(output,'output.txt');
reset(input);rewrite(output);
main;
close(input);close(output);
end.
NOI2003 文本编辑器editor的更多相关文章
- 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操作的字符串中 ...
- AHOI2006文本编辑器editor
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1885 Solved: 683[Submit ...
- 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay
[BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...
- 洛谷 P4008 [NOI2003]文本编辑器 解题报告
P4008 [NOI2003]文本编辑器 题目描述 很久很久以前,\(DOS3.x\)的程序员们开始对 \(EDLIN\) 感到厌倦.于是,人们开始纷纷改用自己写的文本编辑器⋯⋯ 多年之后,出于偶然的 ...
- 【BZOJ】【1269】【AHOI2006】文本编辑器editor
Splay Splay序列维护的模板题了……为了便于处理边界情况,我们可以先插入两个空格当作最左端和最右端,然后……其实本题主要考察的就是Build.splay和Findkth这三个操作,我们可以实现 ...
- BZOJ 1269: [AHOI2006]文本编辑器editor( splay )
splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...
- BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1213 Solved: 454[Submit ...
- BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4633 Solved: 1782 [Sub ...
随机推荐
- ios Object Encoding and Decoding with NSSecureCoding Protocol
Object Encoding and Decoding with NSSecureCoding Protocol February 27, 2014 MISC NSCoding is a fanta ...
- iOS--检测野指针
定位野指针除了使用Malloc Scribble(内存涂鸦)外,还可以使用僵尸对象.所谓的僵尸对象,就是将被释放的对象标记为僵尸,系统不会回收这些对象的内存,并让这些内存无法被重用,因而也就不会被覆写 ...
- OC - 12.NSURLRequest与NSURLConnection
##NSURLRequest NSURLRequest封装了一次网络请求所需要的数据,主要封装了以下信息: 请求路径(URL) 请求方法(GET或POST) 请求头 请求体 超时参数 NSURLReq ...
- java web-----servelet
1,定义servlet: 2,servlet生命周期: 3,HttpServlet常用方法: 4,servlet常用接口: 一,servlet定义: servlet是一个继承HttpServlet类的 ...
- 10_Mybatis开发Dao方法——mapper代理实现
[工程截图(几个关键的标红框)] [UserMapper.xml] <?xml version="1.0" encoding="UTF-8"?> & ...
- shell脚本操作mysql数据库—创建数据库,在该数据库中创建表(插入,查询,更新,删除操作也可以做)
#!/bin/bash HOSTNAME="192.168.1.224" #数据库Server信 ...
- ci 多个文件同时上传
// 单个文件请手册,这里多个文件中,参数设置可参考手册 view 视图 <form...> <input type="file" name="user ...
- Linux下使用openvpn客户端
安装 root 权限用户下 :yum install openvpn fedora23的yum可能会切换到dnf源上安装不必在意,等待就ok. 稍等片刻将自动安装好openvpn需要的软件包.安装完成 ...
- margin系列之内秀篇
本系列摘自 飘零雾雨的博客 最Cool的利器 一样东西在不同的场景,不同的人手里,所能做的事会有很大不同.我深切的以为 margin 绝对是 CSS 中最有能力的利器之一,不知大家以为然否? 前面几 ...
- apache2.2+PHP5.4.28
搭建apache+php开发环境,apache一路正常安装,但是,下载的php搭建后,配置好apache.php,始终报错“The requested operation has failed!”换了 ...