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 ...
随机推荐
- Canvas实现曲线运动
前言 Html5添加的最受欢迎的功能就是<canvas>元素,它负责在页面中设定一个区域,然后在里面可以通过javascript动态地在其内绘制图形. 主流浏览器,IE9+,手机端都是支持 ...
- IP-MAC绑定导致网络故障
前段时间将一台服务器A的服务迁移至了另外一台服务器B,外网IP地址也顺带迁移过来了,结果网络出现了问题. 其中内网是畅通的,但是外网IP怎么都连不上另外一台路由C(B和C是在一个交换机下的,网段也相同 ...
- Java获取某年某周的最后一天
package test; import java.text.SimpleDateFormat; import java.util.Calendar; /** * ClassName: LastDay ...
- prototype原型链继承
依旧是恶补js基础,上代码: 1.定义父类及父类方法 function Animal(){ this.name = "动物"; } Animal.prototype.eat = f ...
- MinGW 仿 linux 开发环境
MinGW 默认安装 MSYS.通常打开的 MinGW Shell 其实 MSYS,MinGW 作为一个组件存在. MSYS -- Minimal SYStem,是一个 Bourne Shell 解释 ...
- WORDPRESS开发(一)自定义页面显示分类目录
第一步:自定义一个页面,如index.php 第二步:打开index.php文件,引用wp-blog-header.php文件 require('wp-blog-header.php'); 第三步使用 ...
- JavaScript—W3school
一.JavaScript基础 1.写入HTML输出 2.对事件作出反应 3.改变HTML内容 4.改变HTML图像 5.改变HTML样式 6.验证输入 <script> Function ...
- jquery阻止事件的两种实现方式
再阻止事件冒泡的方面,jquery有两种方式: 一种是 return false;另外一种是 e.stopPropagation() html代码 <form id="form1&qu ...
- java 文件类操作(转载)
11.3 I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程 ...
- Github readme语法-- markdown
README 该文件用来测试和展示书写README的各种markdown语法.GitHub的markdown语法在标准的markdown语法基础上做了扩充,称之为GitHub Flavored Mar ...