hihoCoder 1228 Mission Impossible 6(rope大法)

题目链接:

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());
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值