题目链接:
http://hihocoder.com/problemset/problem/1228
解题思路:
超长的英文阅读题,只要题意没理解错,就可以模拟出来。。。
题目大意:
定义一些文本编辑器的基本操作
L:光标左移
R:光标右移
S:按下insert
D:按下delete
B:按下backspace,跟三次元不同的是shift操作对它无影响
C:第一次按下相当于shift,第二次按下相当于ctrl+c。跟三次元不同的是ctrl+c时,若光标没有选择字符,则相当于对剪切板进行清
空。在三次元里则是对剪切板无影响。
V:相当于ctrl+v
其它字符:输入这个字符
文本编辑器最大可容纳字符长度为M,若输入操作跟V操作使得新串大于M,则此操作无效。
最后输出结果即可。
算法思想:
我是借鉴一个牛人的代码弄得:
http://paste.ubuntu.com/12500406/
有关rope的操作,请看我的上一篇博客:
http://blog.csdn.net/piaocoder/article/details/48720007
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <ext/rope>
#include <algorithm>
using namespace std;
using __gnu_cxx::crope;
const int maxn = 10010;
char op[maxn];
crope text, board;
int T, m, cbeg, pos, mode;
void change(char c){
int l = text.length(), bl = board.length();
if(c == 'L'){
pos = max(0, pos - 1);
}
else if(c == 'R'){
pos = min(l, pos + 1);
}
else if(c == 'S'){
mode ^= 1;
cbeg = -1;
}
else if(c == 'D'){
if(cbeg == -1){
if(pos < l)
text.erase(pos, 1);
}
else{
if(cbeg < pos)
text.erase(cbeg, pos - cbeg);
if(pos < cbeg)
text.erase(pos, cbeg - pos);
cbeg = -1;
}
}
else if(c == 'B'){
if(pos > 0 && l > 0){
text.erase(pos - 1, 1);
pos--;
}
cbeg = -1;
}
else if(c == 'C'){
if(cbeg == -1){
cbeg = pos;
}
else{
if(cbeg < pos)
board = text.substr(cbeg, pos - cbeg);
if(pos < cbeg)
board = text.substr(pos, cbeg - pos);
if(cbeg == pos)
board.clear();
cbeg = -1;
}
}
else if(c == 'V'){
if(mode == 0 && bl + l <= m){
text = text.substr(0, pos) + board + text.substr(pos, l - pos);
pos += bl;
}
if(mode == 1 && pos + bl < m){
text.erase(pos, min(bl, l - pos));
text = text.substr(0, pos) + board + text.substr(pos, l - pos);
pos += bl;
}
cbeg = -1;
}
else{
if(mode == 0 && l < m){
text = text.substr(0, pos) + c + text.substr(pos, l - pos);
pos++;
}
if(mode == 1 && pos < m){
if(pos < l)
text.erase(pos, 1);
text = text.substr(0, pos) + c + text.substr(pos, l - pos);
pos++;
}
cbeg = -1;
}
//printf("#debug %c %s\n", c, text.c_str());
}
int main() {
scanf("%d",&T);
while(T--){
scanf("%d%s",&m,op);
text.clear();
board.clear();
cbeg = -1;
pos = 0;
mode = 0;
for(int i = 0; op[i]; i++)
change(op[i]);
if((int)text.length() == 0)
puts("NOTHING");
else
printf("%s\n", text.c_str());
}
}