记录编号 376298 评测结果 AAAAAAAAAA
题目名称 [NOI 2003]文本编辑器 最终得分 100
用户昵称 GravatarKZNS 是否通过 通过
代码语言 C++ 运行时间 1.625 s
提交时间 2017-02-26 23:21:06 内存使用 0.31 MiB
显示代码纯文本
//KZNS
#include <cstdio>
#include <list>
#include <memory.h>
using namespace std;
#define Bsize 5000
class block { public:
	int len;
	char s[Bsize];
	block() {
		len = 0;
	}
};
typedef list<block>::iterator CIT;
class editor { public:
	list<block> ls;
	list<block>::iterator cit;
	int clk, clen;
	editor() {
		ls.push_front(block());
		cit = ls.begin();
		clk = 0;
		clen = 0;
	}
	void Split(CIT it, int lk) {
		if (lk == it->len)
			return;
		else {
			block u;
			u.len = it->len - lk;
			memcpy(u.s, it->s+lk, u.len);
			it->len = lk;
			ls.insert(++it, u);
		}
	}
	void Move(int a) {
		clen = a;
		for (CIT i = ls.begin(); i != ls.end(); i++) {
			if (a <= i->len) {
				cit = i;
				clk = a;
				break;
			}
			else {
				a -= i->len;
			}
		}
	}
	void Insert(int n) {
		Split(cit, clk);
		CIT u;
		char c;
		u = cit;
		CIT uu;
		for (int i = 0; i < n; i++) {
			do {
				c = getchar();
			} while (c < 32 || c > 126); 
			if (u->len == Bsize) {
				uu = u;
				ls.insert(++u, block());
				u = ++uu;
			}
			u->s[u->len++] = c; 
		}
	}
	void Delete(int n) {
		Split(cit, clk);
		CIT it = cit;
		for (it++; it != ls.end(); it++) {
			if (it->len < n) {
				n -= it->len;
			}
			else {
				Split(it, n);
				break;
			}
		}
		CIT bbb = cit;
		ls.erase(++bbb, ++it);
	}
	void Get(int n) {
		CIT it = cit;
		for (int i = clk; n && i < it->len; i++) {
			printf("%c", it->s[i]);
			n--;
		}
		for (it++; n && it != ls.end(); it++) {
			for (int i = 0; n && i < it->len; i++) {
				printf("%c", it->s[i]);
				n--;
			}
		}
		printf("\n");
	}
	void Prev() {
		clk--;
		if (!clk) {
			cit--;
			clk = cit->len;
		}
		clen--;
	}
	void Next() {
		clk++;
		if (clk > cit->len) {
			cit++;
			clk = 1;
		}
		clen++;
	}
	void Maintain() {
		CIT j, k;
		for (CIT i = ls.begin(); i != ls.end(); i++) {
			j = i;
			j++;
			if (j == ls.end()) {
				break;
			}
			else if (i->len + j->len <= Bsize) {
				memcpy(i->s+i->len, j->s, j->len);
				i->len += j->len;
				k = j;
				k++;
				ls.erase(j, k);
			}
		}
		Move(clen);
	}
};
char S[20];
int main() {
	freopen("editor2003.in", "r", stdin); 
	freopen("editor2003.out", "w", stdout); 
	editor txt;
	int T;
	scanf("%d", &T);
	int a;
	for (int i = 0; i < T; i++) {
		scanf("%s", S);
		if (S[0] == 'M') {
			scanf("%d", &a);
			txt.Move(a);
		}
		else if (S[0] == 'I') {
			scanf("%d", &a);
			txt.Insert(a);
		}
		else if (S[0] == 'D') {
			scanf("%d", &a);
			txt.Delete(a);
		}
		else if (S[0] == 'G') {
			scanf("%d", &a);
			txt.Get(a);
		}
		else if (S[0] == 'P') {
			txt.Prev();
		}
		else if (S[0] == 'N') {
			txt.Next();
		}
		txt.Maintain();
	}
	return 0;
}
//UBWH