[字符串] [NOI2003]文本编辑器 (洛谷P4008)

这道题居然可以用string暴力A掉…… 提交记录 内网OJ

// luogu-judger-enable-o2
#include <iostream>
#include <string>
#include <regex>
int cursor;
int operations, operationValue, textLength;
std::string text, operationType, operationText, finalText;
char currentChar;
int main() {
    std::ios::sync_with_stdio(false);
    std::cin >> operations;
    while (operations--) {
        std::cin >> operationType;
        if (operationType == "Move") {
            std::cin >> operationValue;
            cursor = operationValue;
        } else if (operationType == "Insert") {
            std::cin >> operationValue;
            finalText = "";
            textLength = 0;
            while (std::cin.get(currentChar)) {
                if (currentChar < 32 || currentChar > 126 || currentChar == '\n' || currentChar == '\r')
                    continue;
                ++ textLength;
                finalText += currentChar;
                if (textLength >= operationValue)
                    break;
            }
            text.insert(cursor, finalText);
        } else if (operationType == "Delete") {
            std::cin >> operationValue;
            text.erase(cursor, operationValue);
        } else if (operationType == "Get") {
            std::cin >> operationValue;
            std::cout << text.substr(cursor, operationValue) << std::endl;
        } else if (operationType == "Prev") {
            if (cursor)
                --cursor;
        } else if (operationType == "Next") {
            if (cursor < text.length())
            ++cursor;
        }
    }
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注