跳到正文

题解

P5734 【深基6.例6】文字处理软件——题解

#SUSPENDED#约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年7月15日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 1 分钟
题目
P5734

版本

  1. v1当前

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

题目分析

这道题目要求我们实现一个简单的文字处理软件,支持四种字符串操作:

  1. 在字符串末尾追加字符串
  2. 截取子字符串
  3. 在指定位置插入字符串
  4. 查找子字符串位置

解题思路

我们可以直接使用C++的string类来操作字符串,因为它已经提供了我们需要的所有功能:

  • append()方法用于在字符串末尾追加内容
  • substr()方法用于截取子字符串
  • insert()方法用于在指定位置插入字符串
  • find()方法用于查找子字符串位置

AC-Code:

C++
#include<bits/stdc++.h>
using namespace std;
int n;
int l,r;
string s,a;
int main(){
	cin>>n;cin>>s;
	for(int opt;n--;){
		cin>>opt;
		if(opt==1){
			cin>>a;
			s.append(a);
			cout<<s<<"\n";
		}else if(opt==2){
			cin>>l>>r;
			s=s.substr(l,r);
			cout<<s<<"\n";
		}else if(opt==3){
			cin>>l>>a;
			s.insert(l,a);
			cout<<s<<"\n";
		}else{
			cin>>a;
			cout<<(int)s.find(a)<<"\n";
		}	
	}
	return 0;
}
原文