Description
这些日子,可可不和卡卡一块儿玩了,原来可可正废寝忘食的想作一个简单而高效的文本编辑器。你能帮助他吗?为了明确任务目标,可可对“文本编辑器”作了一个抽象的定义:
文本:由0个或多个字符构成的序列。这些字符的ASCII码在闭区间[32, 126]内,也就是说,这些字符均为可见字符或空格。
光标:在一段文本中用于指示位置的标记,能够位于文本的第一个字符以前,文本的最后一个字符以后或文本的某两个相邻字符之间。
文本编辑器:为一个能够对一段文本和该文本中的一个光标进行以下七条操做的程序。若是这段文本为空,咱们就说这个文本编辑器是空的。
编写一个程序: 创建一个空的文本编辑器。 从输入文件中读入一些操做指令并执行。 对全部执行过的GET操做,将指定的内容写入输出文件。html
Input
输入文件中第一行是指令条数N,如下是须要执行的N个操做。除了回车符以外,输入文件的全部字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。ios
Output
依次对应输入文件中每条GET指令的输出,不得有任何多余的字符。编辑器
Sample Input
10
Insert 13
Balanced eert
Move 2
Delete 5
Next
Insert 7
editor
Move 0
Get
Move 11
Rotate 4
Getui
Sample Output
B
tspa
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操做不会把光标移动到非法位置。 输入文件没有错误。3d
首先咱们须要写出一道前置题[NOI2003]Editor,而后这题牵涉到翻转子串,那样咱们开两个rope,一个正的,一个反的,而后翻转就成了交换子串了,对吧?code
/*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]=='\n'||s[i]=='\r') 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\n",rop1.at(pos)); if (type[0]=='P') pos--; if (type[0]=='N') pos++; } return 0; }