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 ...
随机推荐
- GitHub Windows客户端部署
下载网址:https://windows.github.com 点击下载按钮即可下载一个小程序,这个小程序会去服务器端下载完整的Windows版客户端,然后自动安装. 如果安装出错的话,那就打开IE浏 ...
- Cassandra CqlRow fetch DateType / Int32Type
phpcassa library里有个namespace : use phpcassa\Schema\DataType; 当使用cql query得到cqlrow, int32 & Date ...
- SqlServer日期(convert函数,getdate函数)
SqlServer日期(convert函数,getdate函数) 函数GETDATE()的返回值在显示时只显示到秒.实际上,SQL Sever内部时间可以精确到毫秒级(确切地说,可以精确到3.33毫秒 ...
- js 中如何通过提示框跳转页面
通过提示框跳转页面 <!doctype html> <html lang="en"> <head> <meta charset=" ...
- lamp环境-编译安装
http://my.oschina.net/JerryBaby/blog/292731 http://blog.chinaunix.net/uid-20639775-id-154442.html ht ...
- 批量执行sql语句
基本使用 $sqls="sql语句1;sql语句2;sql语句n"; 或 $sqls="insert into xx;"; $sqls.="inse ...
- css小心德
内部块最好用百分比,微调时方便
- Ionic简介和环境安装
什么是Ionic Ionic是一个用来开发混合手机应用的,开源的,免费的代码库.可以优化html.css和js的性能,构建高效的应用程序,而且还可以用于构建Sass和AngularJS的优化.ioni ...
- Android 自定义RadioButton实现
由于使用小米系统MIUI运行是RadioButton样式跟google Android API自定义的不一样,则我们可以定义任何想要的东东.没有做不到,只有想不到 Android 自定义RadioBu ...
- Log4j与common-logging
Log4j与common-logging 总网上搜了些Log4j与common-logging的介绍,记录下. 一.Log4j 1.简介 Log4j是Apache的一个开放源代码项目 使用Log4j ...