求调QWQ

P5734 【深基6.例6】文字处理软件

Yaoshui_lv @ 2024-08-15 22:21:40

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    for (int i = 0; i < n; i++) {
        int m;
        cin >> m;
        if (m == 1) {
            string t;
            cin >> t;
            s += t;
            cout << s << endl;
        } else if (m == 2) {
            int a, b;
            cin >> a >> b;
            s = s.substr(a, b);
            cout << s << endl;
        } else if (m == 3) {
            int a; string t;
            cin >> a >> t;
            s.insert(a-1, t);
            cout << s << endl;
        } else if (m == 4) {
            string t;
            cin >> t;
            if (s.find_first_of(t) != string::npos) {
                cout << s.find_first_of(t)+1 << endl;
            } else {
                cout << -1 << endl;
            }
        }
    }
    return 0;
}

by Gcc_Gdb_7_8_1 @ 2024-08-15 23:00:00

@Yaoshui_lv 题目说了: 可以认为文档开头是第 0 个字符

另外。本人发现不能用find_first_of函数,只能用find函数

所以
cout << s.insert(a-1, t); << endl;
要改为:
cout << s.insert(a, t); << endl;

cout << s.find_first_of(t)+1 << endl;
要改为:
cout << s.find(t) << endl;


by Gcc_Gdb_7_8_1 @ 2024-08-15 23:00:55

AC Code:

#include <bits/stdc++.h>
using namespace std;

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

by Gcc_Gdb_7_8_1 @ 2024-08-15 23:01:22

球互关


by Yaoshui_lv @ 2024-08-16 10:40:16

@Gcc_Gdb_7_8_1 关了哈


|