题目的示例过了,但是交上去是0分。求求大佬
查看原帖
题目的示例过了,但是交上去是0分。求求大佬
1790003
huabosong楼主2025/7/24 18:10
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

void keepSubstringInPlace(string& str, size_t a, size_t b) {
    if (a == 0 || a > str.size()) {
        str.clear();
        return;
    }
    size_t start = a; 
    size_t end = start + b;
    if (end > str.size()) {
        end = str.size();
    }
    str.erase(str.begin() + end, str.end());
    str.erase(str.begin(), str.begin() + start);
}

int main() {
    int n;
    string str;
    
    cin >> n;
    cin.ignore();
    getline(cin, str);

    for (int i = 0; i < n; i++) {
        string line;
        getline(cin, line);
        stringstream ss(line);
        string token;
        vector<string> parts;

        while (ss >> token) {
            parts.push_back(token);
        }

        if (parts.empty()) continue;

        if (parts[0] == "1") {
            if (parts.size() < 2) continue;
            str += parts[1];
            cout << str << endl;
        }
        else if (parts[0] == "2") {
            if (parts.size() < 3) continue;
            int a = stoi(parts[1]);
            int b = stoi(parts[2]);
            keepSubstringInPlace(str, a, b);
            cout << str << endl;
        }
        else if (parts[0] == "3") {
            if (parts.size() < 3) continue;
            int pos = stoi(parts[1]);
            if (pos >= 0 && pos <= str.size()) {
                str.insert(pos, parts[2]);
                cout << str << endl;
            }
        }
        else if (parts[0] == "4") {
            if (parts.size() < 2) continue;
            size_t found = str.find(parts[1]);
            if (found != string::npos) {
                cout << found << endl;
            }
            else {
                cout << "-1" << endl;
            }
        }
    }

    return 0;
}
2025/7/24 18:10
加载中...