hihocoder 1228 Mission Impossible 6(模拟文本编辑器)

题意:

题目定义了几种操作:
L:光标左移,假如已经在最左边则不动
R:光标右移动,假如已经在右边则不动
S:切换模式,先是插入模式,后是覆盖模式
D:删除右边, 或者删除位置C-D
B:删除左边
C:复制位置C-C。且两个CC之间不能够存在除L,R以外的字符
V:粘贴,是覆盖模式那么把当前剪切板的内容,从当前光标的位置开始进行覆盖,如果是插入模式,那么就把剪切板的内容插入进光标中。
注意:任何操作,如果超出了文本上限,那么不执行。
C的starting模式下,除了L,R操作以外的任何操作,都会把starting变为nothing。

解析:

就按照题意进行模拟好了,可恶啊比赛的时候,string 的 erase方法写错了,里面直接传了地址,忘记后面跟的长度了,导致一直WA,看不出来。

my code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
using namespace std;

string edit, buf, cmd;
int limit;

int position;
// signal position

int modeType; 
// 0 insert
// 1 overWrite 

int copyType, copyPos;
// copyType = 0 nothing
// copyType = 1 start

void move(char ch) {
    if(ch == 'L') {
        if (position > 0) position--;
    }
    if(ch == 'R') {
        if(position < edit.length()) position++;
    }
}

inline void switchMode() { modeType ^= 1; }

void copyOper() {
    int from, len;
    if(copyType == 0) {
        copyPos = position;
        copyType = 1;
    }else {
        copyType = 0;
        from = min(copyPos, position);
        len = abs(copyPos - position);
        buf = edit.substr(from, len);
    }
}

void backSpace() {
    copyType = 0;
    if(position == 0) return ;
    position--;
    edit.erase(position, 1);
}

void Delete() {
    int len, from;
    if(copyType == 1) {
        len = abs(copyPos - position);
        from = min(position, copyPos);
        edit.erase(from, len);
    }else {
        if (position < edit.length())
            edit.erase(position, 1);
    }
}

void addEdit(char ch) {
    if(modeType == 0) {
        if(edit.length() >= limit) return ;
        edit.insert(position, 1, ch);
        position++;
    }else if(modeType == 1) {
        if(position >= edit.length()) {
            if (edit.length() < limit) {
                edit += ch;
                position++;
            }
        }else {
            edit[position] = ch;
            position++;
        }
    }
}

void paste() {
    if(buf.length() == 0) return ;
    if(modeType == 0) {
        if(edit.length() + buf.length() > limit) return ;
        edit.insert(position, buf);
        position += buf.length();
    }else if(modeType == 1) {
        if(position + buf.length() > limit) return ;
        edit.replace(position, buf.length(), buf);
        position += buf.length();
    }
}

void init() {
    position = copyPos = copyType = modeType = 0;
    edit = buf = cmd = "";
}

void doit(char ch) {
    if(ch == 'L' || ch == 'R') {
        move(ch);
    }else if(ch == 'S') {
        switchMode();
        copyType = 0;
    }else if(ch == 'B') {
        backSpace();
        copyType = 0;
    }else if(ch == 'D') {
        Delete();
        copyType = 0;
    }else if(ch == 'C') {
        copyOper();
    }else if(ch == 'V') {
        paste();
        copyType = 0;
    }else if (ch >= 'a' && ch <= 'z'){
        addEdit(ch);
        copyType = 0;
    }
}

int main() {
    int T;
    cin >> T;
    while(T--) {
        init();
        cin >> limit >> cmd;
        for(int i = 0; i < cmd.length(); i++) {
            doit(cmd[i]);
        }
        puts(edit.length() == 0 ? "NOTHING" : edit.c_str());
    }
    return 0;
}

你可能感兴趣的:(hihoCoder,1228)