机试题——编辑器

题目描述

该编辑器有以下功能:

  1. 插入 :insert str。表示将字符串str插入到当前游标所处位置,同时游标移动到str的右边。
  2. 删除 :delete len。表示将游标左边长度为len的字符串删除。要求该功能命令合法,即len≥0,如果len<0或者len大于字符串长度,则认为输入非法,不进行操作。
  3. 移动 :move cnt。将游标移动cnt次,如果为负数,向左移动,为正数,向右移动。如果cnt超过字符串左右边界,那么认为非法,不进行移动。
  4. 复制 :copy。将游标左边字符串复制并插入到游标的右边。游标位置不变。

输入描述

每行仅输入一个功能对应的操作。如果为end,代表操作结束。

初始时,字符串为空。游标位置为0。

  • 1 ≤ str.length ≤ 40
  • 1 ≤ len ≤ 40
  • -40 ≤ cnt ≤ 40
  • 调用insert,delete,move和copy的总次数不超过200次。

输出描述

最终的文本结果,注意,结果应当包含游标,用|表示。

用例输入

bash 复制代码
insert test
insert pass
move 10
delete 4
insert fail
move -4
copy
end
bash 复制代码
test|testfail

解题思路

  1. 数据结构设计 :
    • 使用一个字符串res来存储当前文本内容。
    • 使用一个整数p表示游标位置。
  2. 功能实现 :
    • 插入 :使用string::insert方法将字符串插入到游标位置,并更新游标位置。
    • 删除:检查删除长度是否合法,如果合法,则从游标左边删除指定长度的字符,并更新游标位置。
    • 移动:检查移动后的位置是否超出边界,如果合法,则更新游标位置。
    • 复制:将游标左边的字符串复制并插入到游标右边,游标位置不变。
  3. 最终输出 :
    • 在游标位置插入|,然后输出整个字符串。

代码

cpp 复制代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<vector>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<set>
#include<list>
#include<sstream>
#include<bitset>
#include<stack>
#include<climits>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string res = "";
    int p = 0; // 游标位置
    string c;
    while (cin >> c && c != "end") {
        if (c == "end") return 0;
        else if (c == "insert") {
            string temp;
            cin >> temp;
            res.insert(p, temp); // 在游标位置插入字符串
            p += temp.size(); // 游标移动到插入字符串的右边
        }
        else if (c == "delete") {
            string temp;
            cin >> temp;
            int len = atoi(temp.c_str());
            if (len > p || len <= 0) continue; // 检查删除操作是否合法
            for (int i = 0; i < len; i++) {
                res.erase(res.begin() + p - 1); // 从游标左边删除字符
                p--; // 游标位置更新
            }
        }
        else if (c == "move") {
            string temp;
            cin >> temp;
            int cnt = atoi(temp.c_str());
            if (p + cnt < 0 || p + cnt > res.size()) continue; // 检查移动操作是否合法
            p += cnt; // 更新游标位置
        }
        else {
            // copy
            string cur = res.substr(0, p); // 获取游标左边的字符串
            res.insert(p, cur); // 将其复制到游标右边
        }
    }
    for (int i = 0; i < res.size(); i++) {
        if (i == p) {
            cout << "|"; // 在游标位置插入 |
        }
        cout << res[i];
    }
    return 0;
}
相关推荐
专注仿真5 小时前
仿真智能体训练(评估篇):基于蒙特卡洛批量对抗的评估算法详解与实战
算法·仿真·评估·afsim智能体·仿真训练
derek19977 小时前
软考软件设计师:B-树和 B+树的区别、线性探查法查找时到底比较了谁
c语言·c++·计算机网络·软件工程·学习方法
fqbqrr8 小时前
2612C++,C++量钢问题
c++
充电zcx9 小时前
C++ 复习:stack、queue 与仿函数——容器适配器、priority_queue 与 deque 的取舍
c++·queue·仿函数·stack·priority_queue
俊俊几年的10 小时前
RK3588 开发板详解:硬件优势与嵌入式 Linux 学习路线
linux·c++·嵌入式
可乐_不渴11 小时前
【C++】 STL篇 哈希表实现
c++·哈希算法·散列表
小O的算法实验室11 小时前
SEVC,城市灾害环境下应急物流的帕累托归档引导式多目标路径规划
算法
维克兜率天11 小时前
【维克】布林带:通道类指标的“量化基石“
人工智能·python·算法
风落青芜11 小时前
C++入门:类和对象(二)
开发语言·数据结构·c++·算法