[AHOI2006] 文本编辑器editor

Description
这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器。你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:


文本:由0个或多个字符构成的序列。这些字符的ASCII码在闭区间[32, 126]内,也就是说,这些字符均为可见字符或空格。
光标:在一段文本中用于指示位置的标记,可以位于文本的第一个字符之前,文本的最后一个字符之后或文本的某两个相邻字符之间。
文本编辑器:为一个可以对一段文本和该文本中的一个光标进行如下七条操作的程序。如果这段文本为空,我们就说这个文本编辑器是空的。
编写一个程序: 建立一个空的文本编辑器。 从输入文件中读入一些操作指令并执行。 对所有执行过的GET操作,将指定的内容写入输出文件。

Input
输入文件中第一行是指令条数N,以下是需要执行的N个操作。除了回车符之外,输入文件的所有字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。

Output
依次对应输入文件中每条GET指令的输出,不得有任何多余的字符。

Sample Input
10
Insert 13
Balanced eert
Move 2
Delete 5
Next
Insert 7
editor
Move 0
Get
Move 11
Rotate 4
Get

Sample Output
B
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操作不会把光标移动到非法位置。 输入文件没有错误。


首先我们需要写出一道前置题[NOI2003]Editor,然后这题牵涉到翻转子串,那样我们开两个rope,一个正的,一个反的,然后翻转就成了交换子串了,对吧?

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<ext/rope>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
using namespace __gnu_cxx;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
	int x=0,f=1;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x>=10)	print(x/10);
	putchar(x%10+'0');
}
crope rop1,rop2;
const int N=2.1e6;
char s[N+10],t[N+10],type[10];
int main(){
	int n=read(),pos=0;
	for (int i=1;i<=n;i++){
		scanf("%s",type);
		if (type[0]=='M')	pos=read();
		if (type[0]=='I'){
			int x=read(),len=rop1.length();
			for (int i=0;i<x;i++){
				s[i]=getchar();
				while (s[i]=='
'||s[i]=='
')	s[i]=getchar();
			}
			s[x]=0;
			rop1.insert(pos,s);
			reverse(s,s+x);
			rop2.insert(len-pos,s);
		}
		if (type[0]=='D'){
			int x=read(),len=rop1.length();
			rop1.erase(pos,x);
			rop2.erase(len-pos-x,x);
		}
		if (type[0]=='R'){
			int x=read(),len=rop1.length();
			rop1.copy(pos,x,s);
			rop2.copy(len-pos-x,x,t);
			s[x]=t[x]=0;
			rop1.replace(pos,x,t);
			rop2.replace(len-pos-x,x,s);
		}
		if (type[0]=='G')	printf("%c
",rop1.at(pos));
		if (type[0]=='P')	pos--;
		if (type[0]=='N')	pos++;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Wolfycz/p/9715542.html