P5734 【深基6.例6】文字处理软件——题解
P5734 【深基6.例6】文字处理软件
题目分析
这道题目要求我们实现一个简单的文字处理软件,支持四种字符串操作:
- 在字符串末尾追加字符串
- 截取子字符串
- 在指定位置插入字符串
- 查找子字符串位置
解题思路
我们可以直接使用C++的string类来操作字符串,因为它已经提供了我们需要的所有功能:
- append()方法用于在字符串末尾追加内容
- substr()方法用于截取子字符串
- insert()方法用于在指定位置插入字符串
- find()方法用于查找子字符串位置
AC-Code:
#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; }