用c++解决下述问题:描述 Given a string S, print the results of the operations. Here are the operations: Insert x i: Insert the character x to the position i; Erase x: Delete all characters x in S; Replace x y: Replace all characters x by y S; Size: Print the size of S; Reverse: Print the reverse of S; Don't change S in this operation; Sub i j: Print the substring of S from position i to j; Find str: Print the starting postion of the "first" str in S; If str is not a substring of S, print -1. 输入 The first line of each test case contains a string S. Then 5 lines follow. The first line includes a character x and an integer i, meaning "Insert x i"; The second line is a character x, meaning "Erase x"; The third line are two characters x and y, meaning "Replace x y"; The fourth line are two integers i and j, meaning "Sub i j"; The last line is a string str, meaning "Find str". All the input characters are English uppercase or lowercase letters. The length of the string will not exceed 100. Inputs will be terminated at the end of the file. 输出 For each test case, you must print "Case i:" in the first line. Then for each operation, print its result in one line. There will be a blank line after each test case.

时间: 2024-03-11 20:51:21 浏览: 16
很高兴为您解答这道题目,以下是C++代码实现: ``` #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string S; int case_num = 1; while (getline(cin, S)) { cout << "Case " << case_num << ":" << endl; case_num++; string x, y, str; int i, j; // Insert x i cin >> x >> i; S.insert(i, x); cout << S << endl; // Erase x cin >> x; S.erase(remove(S.begin(), S.end(), x[0]), S.end()); cout << S << endl; // Replace x y cin >> x >> y; replace(S.begin(), S.end(), x[0], y[0]); cout << S << endl; // Size cout << S.size() << endl; // Reverse reverse(S.begin(), S.end()); cout << S << endl; reverse(S.begin(), S.end()); // Sub i j cin >> i >> j; cout << S.substr(i, j-i+1) << endl; // Find str cin >> str; int pos = S.find(str); if (pos == string::npos) { cout << -1 << endl; } else { cout << pos << endl; } // Blank line cout << endl; cin.ignore(); } return 0; } ``` 该代码使用了字符串的 STL 库,包括 `insert`,`erase`,`replace`,`size`,`reverse`,`substr` 和 `find` 函数。其中,`erase` 函数使用了 `remove` 函数来删除所有字符 x。 程序的输入方式是使用 `getline` 函数读取一行字符串,然后使用 `cin` 读取操作所需的参数。最后,使用 `cout` 输出结果,并在每个测试用例之间输出一个空行。 希望能够帮到您!

相关推荐

最新推荐

recommend-type

C++ 中boost::share_ptr智能指针的使用方法

主要介绍了C++ 中boost::share_ptr智能指针的使用方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
recommend-type

C++ boost::asio编程-异步TCP详解及实例代码

主要介绍了C++ boost::asio编程-异步TCP详解及实例代码的相关资料,需要的朋友可以参考下
recommend-type

C++ boost::asio编程-同步TCP详解及实例代码

主要介绍了C++ boost::asio编程-同步TCP详解及实例代码的相关资料,需要的朋友可以参考下
recommend-type

C++递归与分治算法解的Red and Black,分形(Fractal)以及Rank the Languages问题详解

这是用C++递归与分治算法编写的关于Red and Black问题 Fractal问题 Rank the Languages问题的文档(包括详解和完整代码)
recommend-type

C++语言:switch语句最详细讲解.pdf

C++语言:switch语句最详细讲解。从switch语句的执行过程,switch语句的注意要点,例题讲解,作业等 。适合于中小学生,信息学爱好者。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。