[AHOI2006] 文本編輯器editor

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;
}
相關文章
相關標籤/搜索