【脑叶公司】绿色的考验 题解

· · 休闲·娱乐

T1 绿色的黎明·疑问

签到题。直接对字符串操作就行。

T2 绿色的正午·理解的过程

善用 erase 函数,记得判断字符串是否为空,不然就会 RE 。

T3 绿色的黄昏·前往何方

T1 和 T2 的结合。

T4 绿色的午夜·终末螺旋

应该有很多人被 10ms 和 128MB 吓到了,但是其实代码非常简单……

#include <iostream>
#include <string>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    string S;
    cin >> N >> S;
    for (int i = 0; i < N; i++) {
        int op;
        cin >> op;
        if (op == 1) {
            int p;
            string s;
            cin >> p >> s;
            if (p >= (int)S.size()) {
                S += s;
            } else {
                S.insert(p, s);
            }
        } else if (op == 2) {
            int l, r;
            cin >> l >> r;
            S.erase(l - 1, r - l + 1);
        } else if (op == 3) {
            cout << S << '\n';
        }
    }
    return 0;
}