牛客练习赛31 D 神器大师泰兹瑞与威穆 解题报告 A

链接:https://ac.nowcoder.com/acm/contest/218/D
来源:牛客网

神器大师泰兹瑞与威穆

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

「只要我拉动绳线,你就得随之起舞。」          ——泰兹瑞

https://uploadfiles.nowcoder.com/files/20181030/309907_1540897331977_Image.ashx

 

       泰兹瑞来到卡拉德许之后,由于他精湛的神器制造技术,可谓是过的如鱼得水。这次,他为自己打造了一个编辑器,称为威穆(Veim)。操作威穆时,有两种模式,具体操作如下。

 

Normal Mode

- 按下 i :进入 Insert Mode
- 按下 f :紧接着一个小写字母 char,若当前光标后(右)方有至少一个 char ,将光标移动到其所在位置,否则不移动。
- 按下 x :删除当前光标所在位的字符,后面的字符均会前移一格。
- 按下 h :将光标向左(前)移动一格,若无法移动就不移动。
- 按下 l :将光标向右(后)移动一格,若无法移动就不移动。
- 若按下了其他字符:无任何效果。

 

Insert Mode

- 按下非 e 小写字母 char :在光标当前位置前插入这个字母 char。
- 按下 e :退出 Insert Mode(进入 Normal Mode)。

       (具体请见样例)

       现在泰兹瑞的威穆中已经写入了一个字符串 s 。接下去泰兹瑞进行了一波操作(按下了若干按键),他的按键序列为 t 。现给出 s 和 t ,求这波操作之后威穆内留下的字符串。

输入描述:

两行,第一行字符串 s ,第二行字符串 t 。

输出描述:

一行,威穆里最后留下的字符串。

示例1

输入

复制

applese

xfllhlia

输出

复制

pplaese

说明

- 初始时,字符串为 ,威穆处于 Normal Mode 。下划线表示光标所在位置。
- 第一步操作为 x ,删除当前光标所在位的字符,并且光标后移一格。字符串变为  。威穆仍处于 Normal Mode。
- 下一步操作为 f ,之后跟有一个字符 `l` 。光标后存在字符 `l` ,故移动到该位置。字符串变为  。威穆仍处于 Normal Mode。
- 下一步操作为 l ,光标后移一格。字符串变为  。威穆仍处于 Normal Mode。
- 下一步操作为 h ,光标前移一格。字符串变为  。威穆仍处于 Normal Mode。
- 下一步操作为 l ,光标后移一格。字符串变为  。威穆仍处于 Normal Mode。
- 下一步操作为 i ,威穆进入 Insert Mode。字符串仍为  。
- 下一步操作为 a ,而非 e ,故插入字符 a 。字符串变为  。

示例2

输入

复制

pppp

iaefpfpia

输出

复制

appapp

备注:

1 ≤ |s|, |t| ≤ 105
s, t 均由小写拉丁字母组成。

 

 

分析

看完题目之后,意识到除了模拟别无他法,于是迅速暴力手写完成,其中normal mode中的f比较迷,,看了半天才看懂,然后提交TLE QAQ

TLE代码:

#include <stdio.h>

#include <math.h>

#include <string.h>

#include <algorithm>

#include <iostream>

#include <string>

#include <time.h>

#include <queue>

#include <string.h>

#define sf scanf

#define pf printf

#define lf double

#define ll long long

#define p123 printf("123\n");

#define pn printf("\n");

#define pk printf(" ");

#define p(n) printf("%d",n);

#define pln(n) printf("%d\n",n);

#define s(n) scanf("%d",&n);

#define ss(n) scanf("%s",n);

#define ps(n) printf("%s",n);

#define sld(n) scanf("%lld",&n);

#define pld(n) printf("%lld",n);

#define slf(n) scanf("%lf",&n);

#define plf(n) printf("%lf",n);

#define sc(n) scanf("%c",&n);

#define pc(n) printf("%c",n);

#define gc getchar();

#define re(n,a) memset(n,a,sizeof(n));

#define len(a) strlen(a)

#define LL long long

#define eps 1e-6

using namespace std;

char s[100500],t[100500];

int main(){

    ss(s);

    ss(t);

    int sta = 0;//0是normal ,1是insert

    int lens = len(s);

    int lent = len(t);

    int x = 0;

    for(int i = 0; i < lent; i ++){

        if(sta == 0){

            if(t[i] == 'i'){

                sta = 1;

            }else if(t[i] == 'f'){

                char c = t[i+1];

                for(int i = x+1; i < lens; i ++){

                    if(s[i] == c){

                        x = i;

                        break;

                    }

                }

 

                i ++;

            }else if(t[i] == 'x'){

                for(int i = x; i < lens; i ++){

                    s[i] = s[i+1];

                }

                lens --;

            }else if(t[i] == 'h'){

                if(x != 0){

                    x --;

                }

            }else if(t[i] == 'l'){

                if(x != lens-1){

                    x++;

                }

            }

        }else{

            if(t[i] == 'e'){

                sta = 0;

            }else{

                for(int i = lens+1; i >= x+1; i --){

                    s[i] = s[i-1];

                }

                s[x] = t[i];

                x ++;

            }

        }

        //ps(s) pn

    }

ps(s) pn

 

}

 

打过一遍之后对题目有很深的理解了,意识到用链表做比较轻松,因为中间存在一个光标,直接调整光标插入删除速度很快,

But, But! But!! But!!!

不会list啊。。。。。。。。

绝望,,,,,自闭了。。。。。

现场学list:

https://www.cnblogs.com/lalalabi/p/5060210.html

https://www.cnblogs.com/loleina/p/5179677.html

https://blog.csdn.net/zhouzhenhe2008/article/details/77428743/

https://blog.csdn.net/amoscykl/article/details/80934961

然后掌握了基本用法之后在题目里还有坑,

需要用到迭代器遍历链表,在检查的时候因为这个改了好多遍,,,还WA了一发,所以标红:如果要遍历检查一定要新建一个迭代器!!!

然后顺利AC

代码:

#include <stdio.h>

#include <math.h>

#include <string.h>

#include <algorithm>

#include <iostream>

#include <string>

#include <time.h>

#include <queue>

#include <string.h>

#include <list>

#define sf scanf

#define pf printf

#define lf double

#define ll long long

#define p123 printf("123\n");

#define pn printf("\n");

#define pk printf(" ");

#define p(n) printf("%d",n);

#define pln(n) printf("%d\n",n);

#define s(n) scanf("%d",&n);

#define ss(n) scanf("%s",n);

#define ps(n) printf("%s",n);

#define sld(n) scanf("%lld",&n);

#define pld(n) printf("%lld",n);

#define slf(n) scanf("%lf",&n);

#define plf(n) printf("%lf",n);

#define sc(n) scanf("%c",&n);

#define pc(n) printf("%c",n);

#define gc getchar();

#define re(n,a) memset(n,a,sizeof(n));

#define len(a) strlen(a)

#define LL long long

#define eps 1e-6

using namespace std;

list<char> l;

char s[100500],t[100500];

int main(){

    //ss(s);

    //ss(t);

    cin >> s >> t;

    int sta = 0;//0是normal ,1是insert

    int lens = len(s);

    int lent = len(t);

    list<char>::iterator x;

    //int x = 0;

    x = l.begin();

    for(int i = 0; i < lens; i ++){

        l.insert(x,s[i]);

    }

 

    /*for(x = l.begin();x!=l.end();++x){

        cout<<*x;

    }*/

    x = l.begin();

    for(int i = 0; i < lent; i ++){

        if(sta == 0){

            if(t[i] == 'i'){

                sta = 1;

            }else if(t[i] == 'f'){

                char c = t[i+1];

                list<char>::iterator y;

                x ++;

                y = x;

                x --;

                for(;y!=l.end();++y){

                    if(*y == c){

                        x = y;

                        break;

                    }

                }

                i ++;

            }else if(t[i] == 'x'){

 

                list<char>::iterator xx;

                xx = x;

                x++;

                l.erase(xx);

 

                //x = l.begin();

            }else if(t[i] == 'h'){

                if(x != l.begin()){

                    x --;

                }

            }else if(t[i] == 'l'){

                if(x != l.end()){

                    x++;

                }

            }

        }else{

            if(t[i] == 'e'){

                sta = 0;

            }else{

                l.insert(x,t[i]);

            }

        }

        /*list<char>::iterator z;

        for(z = l.begin();z!=l.end();++z){

            cout<<*z;p123

        }

        cout << endl;*/

        //ps(s) pn

    }

    //ps(s) pn

    list<char>::iterator z;

    for(z = l.begin();z!=l.end();++z){

        cout<<*z;

    }

    cout << endl;

 

 

}

猜你喜欢

转载自blog.csdn.net/qq_41159195/article/details/84565507