hihocoder 1228 Mission Impossible 6 一道写的我蛋碎的模拟题


http://hihocoder.com/problemset/problem/1228

前天网赛遇到的一道模拟UNIX下文本操作的题。
题目如下。。

#1228 : 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

比赛时算错复杂度了。。用链表模拟了此题,优化了时间。

每个指针都相当于   |a|b|c|d|  中的'|', 指针的c 表示指针后面所跟的字母。

然后暴力模拟。。试了试,应该比非内存操作的方法时间更快。虽然写的好丑。。。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct node{char c;node *pre;node *next;}*root,*f;
char que[10010],fz[10010],xu[10010];
int st,ed,pos,fzn,xun,cnt,flag,cflag,T,m;
void dx(char st)    //向后复写
{
    if(f->next==NULL)
    {
        if(cnt==m) return;
        cnt++;
        f->next=new node();
        f->next->pre=f;
        f->next->c='\0';
        f->next->next = NULL;
    }
    pos++;
    f->c=st;
    f=f->next;
}
void dc(char st)    //向后插入
{
    if(cnt==m) return;
    cnt++;
    node *ff=new node();
    ff->c=st;
    ff->next=f;
    ff->pre=f->pre;
    if(f==root)
        root=ff;
    else f->pre->next=ff;
    f->pre=ff;
    pos++;
}
void bs()   //向前删除
{
    if(cflag==1)
        cflag=0;
    if(f==root)
        return;
    pos--;
    cnt--;
    if(f->pre==root)
    {
        node* ff=root;
        root=f;
        delete(ff);
        return;
    }
    f->pre->pre->next=f;
    node *ff=f->pre;
    f->pre=f->pre->pre;
    delete(ff);

}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&m);
        scanf("%s",que);
        root = new node();
        root->next=NULL;root->pre=NULL;root->c='\0';
        cnt=0,flag=0,cflag=0;st=0;ed=0;fzn=0;xun=0;pos=0;
        f=root;        
        for(int i=0;que[i]!='\0';i++)
        {
            if(que[i]>='a'&&que[i]<='z')
            {
                if(cflag==1)
                {
                    cflag=0;
                }
                if(flag==1)
                    dx(que[i]);
                else
                    dc(que[i]);
            }
            else if(que[i]=='C')
            {
                if(cflag==0)
                {
                    st=pos;ed=pos;
                    xun=0;
                    cflag=1;
                }
                else
                {
                    xu[xun]='\0';
                    if(st<=ed)
                        strcpy(fz,xu);
                    else
                        for(int j=xun-1;j>=0;j--)
                            fz[xun-1-j]=xu[j];
                    fzn=xun;
                    fz[fzn]='\0';
                    cflag=0;
                }
            }
            else if(que[i]=='L')
            {
                if(f==root)
                    continue;
                if(cflag==1)
                {
                    if(ed>st)
                        xun--;
                    else
                        xu[xun++]=f->pre->c;
                }
                f=f->pre;
                pos--;
                ed--;
            }
            else if(que[i]=='R')
            {
                if(f->next==NULL)
                    continue;
                if(cflag==1)
                {
                    if(ed<st)
                        xun--;
                    else
                        xu[xun++]=f->c;
                }
                f=f->next;
                pos++;
                ed++;
            }
            else if(que[i]=='S')
            {
                if(cflag==1)
                    cflag=0;
                flag=(!flag);
            }
            else if(que[i]=='D')
            {
                if(cflag==1)
                {
                    if(ed==st)
                        continue;
                    if(ed>st)
                    {
                        while(ed!=st)
                        {
                            ed--;
                            pos--;
                            cnt--;
                            if(f->pre==root)
                            {
                                node *ff=root;
                                root=f;
                                delete(ff);
                                continue;
                            }
                            f->pre->pre->next=f;
                            node *fff=f->pre;
                            f->pre=f->pre->pre;
                            delete(fff);
                        }
                    }
                    else
                    {
                        while(ed!=st)
                        {
                            ed++;
                            cnt--;
                            if(f==root)
                            {
                                f=f->next;
                                node* fff=root;
                                root=f;
                                delete(fff);
                                continue;
                            }
                            f->pre->next=f->next;
                            f->next->pre=f->pre;
                            node *ff=f;
                            f=f->next;
                            delete(ff);
                        }

                    }
                    cflag=0;
                }
                else 
                {
                    if(f->next==NULL){
                        continue;
                    }
                    cnt--;
                    if(f==root)
                    {
                        f=f->next;
                        node* fff=root;
                        root=f;
                        delete(fff);
                        continue;
                    }
                    f->pre->next=f->next;
                    f->next->pre=f->pre;
                    node *ff=f;
                    f=f->next;
                    delete(ff);
                }
            }
            else if(que[i]=='B')
                bs();
            else if(que[i]=='V')
            {
                if(cflag==1)
                    cflag=0;
                if(fzn==0)
                    continue;
                if(flag==0)
                {
                    if(cnt+fzn>m) continue;
                    for(int j=0;j<fzn;j++)
                        dc(fz[j]);
                }
                else
                {
                    if(cnt-pos<=fzn)
                    {
                        if(cnt-pos+fzn>m)
                            continue;
                        node *ff=f->next;
                        while(ff!=NULL)
                        {
                            node *fff=ff;
                            delete(fff);
                            ff=ff->next;
                        }
                        f->next=NULL;
                        f->c='\0';
                        for(int j=0;j<fzn;j++)
                            dc(fz[j]);
                    }
                    else
                    {
                        for(int j=0;j<fzn;j++)
                        {
                            f->c=fz[j];
                            f=f->next;
                            pos++;
                        }
                    }
                }
            }
        }
        node *sd=root;
        if(sd->next==NULL)
        {
            puts("NOTHING");
            continue;
        }
        while(sd->next!=NULL)
        {
            printf("%c",sd->c);
            sd=sd->next;
        }
        puts("");
        while(root)
        {
            node *rro=root;
            root=root->next;
            delete(rro);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值