题意

模拟一个打字的操作,最初打了一段字,光标的位置在这段字的第一个字符处,然后你需要执行下边一系列操作。

在 Normal Mode 下

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

在 Insert Mode 下

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

题解

拿两个栈进行模拟就行了,首先把字符串都加入到第二个栈中,光标就是处于第一个栈和第二个栈之间。然后进行模拟操作就行了。

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
char a[N], b[N];
char s[N], t[N];
int topa, topb;
int main() {
    int mode = 0, m;
    scanf("%s%s", s, t);
    int n = strlen(s);
    for (int i = n - 1; i >= 0; i--) b[topb++] = s[i];
    for (int i = 0, m = strlen(t); i < m; i++) {
        if (!mode) {
            if (t[i] == 'i') mode = 1;
            else if (t[i] == 'f') {
                i++;
                if (i < m && t[i]) {
                    for (int j = topb - 2; j >= 0; j--) {
                        if (b[j] == t[i]) {
                            int ti = topb - j - 1;
                            while (topb && ti--) a[topa++] = b[--topb];
                            break;
                        }
                    }
                }
            }
            else if (t[i] == 'x') {
                if (topb) --topb;
            }
            else if (t[i] == 'h') {
                if (topa) b[topb++] = a[--topa];
            }
            else if (t[i] == 'l') {
                if (topb) a[topa++] = b[--topb];
            }
        }
        else {
            if (t[i] == 'e') mode = 0;
            else {
                a[topa++] = t[i];
            }
        }
    }
    for (int i = 0; i < topa; i++) printf("%c", a[i]);
    for (int i = topb - 1; i >= 0; i--) printf("%c", b[i]);
    printf("\n");
    return 0;
}