跳到正文

休闲·娱乐

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

HellWriter约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年2月11日
收录
2026年2月11日
更新
2026年11月11日
篇幅
约 2 分钟

版本

  1. v1当前

T1 绿色的黎明·疑问

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

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

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

T3 绿色的黄昏·前往何方

T1 和 T2 的结合。

T4 绿色的午夜·终末螺旋

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

C++
#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;
}
原文