2015 ACM/ICPC 北京赛区网络赛 B.Mission Impossible 6 解题报告


 Mission Impossible 6

时间限制:1000ms

单点时限: 1000ms
内存限制: 256MB

描述

You must have seen the very famous movie series,"Mission Impossible", from 1 to 4. And "Mission Impossible 5" is now on screen in China. Tom Cruise is just learning programming through my MOOC course, and he wants a good score. So I made him divulge the story of "Mission Impossible 6".

In "Mission Impossible 6",  Ethan Hunt risks his life to install a mini camera in Bad Boss's room, in order to peep at the work Bad Boss does on his computer. Unfortunately, Bad Boss moves his desk to get more sunshine, and after that Ethan can't see the computer screen through the camera. Fortunately, Ethan can still see the keyboard. So, Ethan wants to know what Bad Boss writes on his computer by watching Bad Boss's keyboard inputs. That job is neither exciting nor risky, so it's really impossible for Ethan to accomplish --- that's why Tom Cruise wants to learn programming.

To simplified the problem, we assume that Bad Boss is editing a one line document, and the document consists of only lowercase letters. At first, there are nothing in the text editor window except a caret blinking at the left-most position (the starting position of the line). When Bad Boss input a lowercase letter, the letter appears at the right side of the caret, and then the caret moves to the right side of the letter just inputted. The text editor can switch between "insert mode" and "overwrite mode". When it's in "overwrite mode", the newly inputted letter will overwrite the letter which is on the right of the caret(if there is one). If it's in "insert mode", the newly inputted letter will be inserted before the letter which is originally on the right of the caret.

Besides inputting lowercase letters, Bad Boss can do some operations by inputting some uppercase letters, as described below:

'L' :  Moves the caret toward left by one letter. If the caret is already at the start of the line, then nothing happens.

'R':  Moves the caret toward right by one letter. If the caret is already at the end of the line(it means that there are no letters on the right side of the caret), then nothing happens.

'S':  Switch between "overwrite mode" and "insert mode". At the beginning, it's in "insert mode".

'D':  Delete a letter which is on the right of the caret. If the caret is already at the end of the line, then nothing happens. 'D' also has other effect which is described in 'C' operation below.

'B':  Delete a letter which is on the left of the caret. If the caret is already at the start of the line, then nothing happens.

'C': Copy something to the clipboard. At first , there is nothing in the clipboard, and the "copy status" of the editor is set to "NOTHING". When key 'C' is pressed:

If copy status is "NOTHING", copy status will be changed into "START", and the current position of caret is saved as "copy position 1".

if copy status is "START ", copy status will be changed into "NOTHING" and the letters between current caret position and the saved "copy position 1" will be copied into clipboard(The old content in the clipboard is replaced). If those two positions are the same, clipboard will be cleared.

Please note that , if any letter except 'L' , 'R' and 'D' is inputted when copy status is "START", copy status will changed into "NOTHING" immediately, not affecting the inputted letter taking its own effect as mention above. If 'D' is inputted when copy status is "START", copy status will changed into "NOTHING" immediately, and the letters between current caret position and "copy position 1" will be deleted.

'V': Paste the content in the clipboard into the right of the caret. If there is nothing in the clipboard, nothing happens. In "insertion mode", the pasted content is inserted. If it's in "overwrite mode" and there are k letters in the clipboard, then k letters will be overwrote. In "overwrite mode", if the number of letters on the right side of the caret is less then k, those letters will also all be replaced by the letters in the clipboard. After the paste operation, the caret moves to the right of the last pasted letter.

The content of the text line will never exceed M letters. Any input which will cause the content exceed M letters must be ignored. Especially, when you paste, you either paste all content in the clipboard, or paste nothing due to the text length limit.

输入

The first line of the input is a integer T(T <= 20), meaning that there are T test cases. The T lines follow, and each line is a test case.

For each test case:

A integer M (0 <= M <= 10,000) goes first ,meaning the text length limitation. Then some letters follow, describing what Bad Boss inputs. The total number of letters in one test case is no more than 10,000.

输出

For each test case, print the result which Bad Boss gets. If the result is nothing, print "NOTHING".

样例输入
8
100 abcdeLCLLD
5 abcLkjff
15 abcBBdeLLDDxzDDDDRRRR
25 abcdefgLLLSxyzSLLku
20 abcdefgLLCkLLCRRRRRCLV
20 abcdefgLLCkLLCRRRRCLLLSV
30 abcdeCLLCRRVCLRCabVkz
10 abcBBBLB
样例输出
abe
abkjc
axz
abcdxkuyz
abcdekfekfgg
abcdeekfg
abcdedeabkz
NOTHING


方法deque模拟,效率应该不是很高,still naive。

#include <bits/stdc++.h>

using namespace std;

deque<char> text;

int main()
{
    int T,M,mode,clipstat,pointer,clipos1,clipos2;
    string cmd,clipboard;
    scanf("%d",&T);
    for(int k=0; k<T; k++) {
        text.clear();
        clipboard.clear();
        clipstat=0;
        mode=0;
        pointer=0;
        cin>>M>>cmd;
        for(auto i:cmd) {
            switch (i) {
            case'L':
                if(pointer!=0) {
                    pointer--;
                }
                break;
            case'R':
                if(pointer!=int(text.size())) {
                    pointer++;
                }
                break;
            case'S':
                if(mode==0) {
                    mode=1;
                } else if(mode==1) {
                    mode=0;
                }
                if(clipstat==1) {
                    clipstat=0;
                }
                break;
            case'D':
                if(clipstat==1) {
                    clipstat=0;
                    //delete{pointer<->clipos1}
                    clipos2=pointer;
                    clipboard.clear();
                    if(clipos2<clipos1) {
                        swap(clipos1,clipos2);
                    }
                    auto iter1=next(text.begin(),clipos1);
                    auto iter2=next(text.begin(),clipos2);
                    text.erase(iter1,iter2);
                    pointer=clipos1;
                } else if(pointer!=int(text.size())) {
                    text.erase(next(text.begin(),pointer));
                }
                break;
            case'B':
                if(clipstat==1) {
                    clipstat=0;
                }
                if(pointer!=0) {
                    text.erase(next(text.begin(),--pointer));
                }
                break;
            case'C':
                if(clipstat==0) {
                    clipstat=1;//clip start
                    clipos1=pointer;
                } else if(clipstat==1) {
                    clipstat=0;
                    if(pointer==clipos1) {
                        clipboard.clear();
                    } else {
                        //clipboard=pointer<->clipos1
                        clipos2=pointer;
                        clipboard.clear();
                        if(clipos2<clipos1) {
                            swap(clipos1,clipos2);
                        }
                        for(auto j=next(text.begin(),clipos1); j!=next(text.begin(),clipos2); j++) {
                            clipboard.push_back(*j);
                        }
                    }
                }
                break;
            case'V':
                if(!clipboard.empty()) {
                    if(mode==1&& ((pointer+int(clipboard.size()))<=M) ) { //overwrite mode
                        for(int j=0; j<int(clipboard.size()); j++) {
                            if(pointer<int(text.size())) {
                                text[pointer++]=clipboard[j];
                            } else {
                                text.push_back(clipboard[j]);
                                pointer++;
                            }
                        }
                    } else if(mode==0&& ((int(text.size())+int(clipboard.size()))<=M) ) { //insert mode
                        for(int j=0; j<int(clipboard.size()); j++) {
                            text.insert(next(text.begin(),pointer++),clipboard[j]);
                        }
                    }
                }
                break;
            default:
                if(int(text.size())<M&&mode==0) {//insert mode
                    text.insert(next(text.begin(),pointer++),i);
                } else if(mode==1) { //overwrite mode
                    if(pointer<int(text.size())) {
                        text[pointer++]=i;
                    } else if(int(text.size())<M){
                        text.push_back(i);
                        pointer++;
                    }
                }
                if(clipstat==1) {
                    clipstat=0;
                }
                break;
            }
        }
        if(int(text.size())!=0)for(auto i:text) {
                cout<<i;
            }
        else {
            cout<<"NOTHING";
        }
        cout<<endl;
    }
    return 0;
}

卡了很多地方,字符上限M达到时的V操作是否合法,转换模式S为复写的时候是插值还是覆盖,M=0的特殊情况(比赛时答疑说明输入没有空串),本来以为正式比赛会卡STL时间的,用了这么多STL容器和函数还有点虚,没想到居然过了……


你可能感兴趣的:(2015 ACM/ICPC 北京赛区网络赛 B.Mission Impossible 6 解题报告)