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

GitHub跳转原题关系图返回列表

用字符串切片实现追加、截取、插入和 find 查找四种文字处理操作。

OJ: luogu

题目 ID: P5734

难度:入门

标签:字符串模拟python

日期: 2026-07-15 20:35

题意

维护一个文档字符串,依次执行 q 次操作:追加、截取、插入、查找。每次操作都要输出对应结果。

思路

四种操作都可以直接对应到 Python 字符串操作:

  • 1 strdocument += str
  • 2 a bdocument = document[a:a+b]
  • 3 a strdocument[:a] + str + document[a:]
  • 4 strdocument.find(str)

数据范围很小,字符串每次新建也能通过。这题是字符串 API 和切片练习,不创建 brute.py

Python 知识

  • /home/rainboy/mycode/hugo-blog/content/program_language/python/input_output_and_strings.md:字符串切片和 find 属于常用字符串操作。
  • /home/rainboy/mycode/hugo-blog/content/program_language/python/oj_input_output_cheatsheet.md:逐行读取操作,保留每行结构。
  • s[l:r] 取左闭右开区间。
  • find 找不到时返回 -1,正好符合题目要求。

代码

python
operation_count = int(input())
document = input().strip()

for _ in range(operation_count):
    parts = input().split()
    operation = parts[0]

    if operation == "1":
        document += parts[1]
        print(document)
    elif operation == "2":
        start = int(parts[1])
        length = int(parts[2])
        document = document[start:start + length]
        print(document)
    elif operation == "3":
        position = int(parts[1])
        fragment = parts[2]
        document = document[:position] + fragment + document[position:]
        print(document)
    else:
        print(document.find(parts[1]))
cpp
/**
 * Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
 * rbook: -> https://rbook.roj.ac.cn  https://rbook2.roj.ac.cn
 * rainboy的学习导航网站: https://idx.roj.ac.cn
 * create_at: 2026-07-27 00:00
 * update_at: 2026-07-27 00:00
 */

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

char doc[100005]; // 文档字符串
int q;            // 操作次数

int main() {
    cin >> q >> doc;
    int op, a, b;
    char str[1005];
    for (int i = 1; i <= q; i++) {
        cin >> op;
        if (op == 1) { // 追加
            cin >> str;
            strcat(doc, str);
            cout << doc << "\n";
        } else if (op == 2) { // 截取子串
            cin >> a >> b;
            doc[a + b] = '\0'; // 在截取终点后截断
            // 将子串移到开头
            memmove(doc, doc + a, b + 1);
            cout << doc << "\n";
        } else if (op == 3) { // 插入
            cin >> a >> str;
            int len_doc = strlen(doc);
            int len_str = strlen(str);
            // 将后面的字符后移
            memmove(doc + a + len_str, doc + a, len_doc - a + 1);
            // 将 str 复制到插入位置
            memcpy(doc + a, str, len_str);
            cout << doc << "\n";
        } else { // 查找
            cin >> str;
            char *p = strstr(doc, str);
            if (p == NULL) cout << -1 << "\n";
            else cout << (p - doc) << "\n";
        }
    }
    return 0;
}

Pythonic 写法

match-case 分发文本操作:

python
operation_count = int(input())
document = input().strip()

for _ in range(operation_count):
    op, *args = input().split()
    match op:
        case "1":
            document += args[0]
            print(document)
        case "2":
            start, length = map(int, args)
            document = document[start : start + length]
            print(document)
        case "3":
            position = int(args[0])
            document = document[:position] + args[1] + document[position:]
            print(document)
        case "4":
            print(document.find(args[0]))

复杂度

设当前文档长度为 L,每次字符串修改或查找最坏为 O(L)O(L)q <= 100 且初始长度很小,可以直接模拟。

总结

文字处理题先把每种指令翻译成一种字符串操作,再注意切片下标是左闭右开。