NOI 2003 文本编辑器 题解

题意:

写一种数据结构,支持任意位置插入、删除、修改。

不想也不会写什么平衡树或者块状数组怎么办?

那就用 STL 自带的 rope 吧!

定义一个变量 (now) 来充当光标。

对于 MovePrevNext 可以进行光标的移动。

对于 Get 操作,暴力输出即可。

对于 InsertDelete 操作,可以使用 rope 自带的函数 inserterase

Code:

#include<bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
rope<char> wbbjq;
char cz;
char kk[2300000];
int t,x,now=0;
inline void reads(char *s, int len) {
    s[len]='';
    len--;
    for(int i=0;i<=len;i++) {
    	s[i]='';
    	while(s[i]<32||126<s[i])
    	    s[i]=getchar();
    }
}
inline void read(int &x) {
    x=0;
	char ch;
	while(!isdigit(ch=getchar()));
	x=ch-'0';
	while(isdigit(ch=getchar()))
		x=x*10+ch-'0';
}//本题的读入需要特别小心,格式十分奇怪,这里使用手写函数
int main() {
    scanf("%d",&t);
    while(t--) {
    	cz='1';
    	while(!isalpha(cz=getchar()));
    	while(isalpha(getchar()));//过滤掉后面的字符
    	if(cz=='M')
    		read(now);
    	else if(cz=='I') {
    		read(x);
    		reads(kk,x);
    		wbbjq.insert(now,kk);
    	}
    	else if(cz=='D') {
    		read(x);
    		wbbjq.erase(now,x);
    	}
    	else if(cz=='G') {
    		read(x);
    		x--;//注意:因为 STL 里的下标均以 0 为起点,而本题以 1 为起点,所以记得减 1 
    		for(int i=now;i<=now+x;i++)
    		    printf("%c",wbbjq[i]);
    		puts("");
    	}
    	else if(cz=='P')
    	    now--;
    	else
    	    now++;
    }
}
原文地址:https://www.cnblogs.com/lajiccf/p/12942423.html