1. 程式人生 > 實用技巧 >洛谷-P5734 【深基6.例6】文書處理軟體

洛谷-P5734 【深基6.例6】文書處理軟體

洛谷-P5734 【深基6.例6】文書處理軟體

原題連結:https://www.luogu.com.cn/problem/P5734


題目描述

你需要開發一款文書處理軟體。最開始時輸入一個字串(不超過 100 個字元)作為初始文件。可以認為文件開頭是第 0 個字元。需要支援以下操作:

  • 1 str:後接插入,在文件後面插入字串 str,並輸出文件的字串。
  • 2 a b:擷取文件部分,只保留文件中從第 a 個字元起 b 個字元,並輸出文件的字串。
  • 3 a str:插入片段,在文件中第 a 個字元前面插入字串 str,並輸出文件的字串。
  • 4 str
    :查詢子串,查詢字串 str 在文件中最先的位置並輸出;如果找不到輸出 -1。

為了簡化問題,規定初始的文件和每次操作中的 str 都不含有空格或換行。最多會有 \(q(q\le100)\) 次操作。

輸入格式

輸出格式

輸入輸出樣例

輸入 #1

4
ILove
1 Luogu
2 5 5
3 3 guGugu
4 gu

輸出 #1

ILoveLuogu
Luogu
LuoguGugugu
3

C++程式碼

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

int main() {
    int n, m, x, y;
    string s, t;
    cin >> n >> s;
    for (int i=0; i<n; ++i) {
        cin >> m;
        if (m == 1) {
            cin >> t;
            s += t;
            cout << s << endl;
        } else if (m == 2) {
            cin >> x >> y;
            s = s.substr(x, y);
            cout << s << endl;
        } else if (m == 3) {
            cin >> x >> t;
            s = s.insert(x, t);
            cout << s << endl;
        } else if (m == 4) {
            cin >> t;
            x = s.find(t);
            cout << x << endl;
        }
    }
    return 0;
}