题目描述
你需要开发一款文字处理软件。最开始时输入一个字符串作为初始文档。可以认为文档开头是第 00 个字符。需要支持以下操作:
1 str
:后接插入,在文档后面插入字符串 \texttt{str}str,并输出文档的字符串。2 a b
:截取文档部分,只保留文档中从第 aa 个字符起 bb 个字符,并输出文档的字符串。3 a str
:插入片段,在文档中第 aa 个字符前面插入字符串 \texttt{str}str,并输出文档的字符串。4 str
:查找子串,查找字符串 \texttt{str}str 在文档中最先的位置并输出;如果找不到输出 -1−1。
为了简化问题,规定初始的文档和每次操作中的 \texttt{str}str 都不含有空格或换行。最多会有 qq 次操作。
输入格式
第一行输入一个正整数 qq,表示操作次数。
第二行输入一个字符串 \texttt{str}str,表示最开始的字符串。
第三行开始,往下 qq 行,每行表示一个操作,操作如题目描述所示。
输出格式
一共输出 nn 行。
对于每个操作 1,2,31,2,3,根据操作的要求输出一个字符串。
对于操作 44,根据操作的要求输出一个整数。
样例数据
输入数据 1
4
hello
1 world
2 5 5
3 3 kkkk
4 kl
输出数据 1
helloworld
world
workkkkld
6
提示
数据保证,1 \leq q\le 1001≤q≤100,开始的字符串长度 \leq 100≤100。
以下是代码:
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
int n;
cin>>n>>s;
while(n--){
int m;
cin>>m;
if(m==1){
string tp;
cin>>tp;
s+=tp;
cout<<s<<endl;
}
else if(m==2){
int a,b;
cin>>a>>b;
s=s.substr(a,b);
cout<<s<<endl;
}
else if(m==3){
int a;
string tp;
cin>>a>>tp;
s.insert(a,tp);
cout<<s<<endl;
}
else{
string tp;
cin>>tp;
int f=s.find(tp);
cout<<f<<endl;
}
}
return 0;
}
c++提供了一种基于vector
容器的字符串数据结构:string。
喜欢的话可以点赞三连哦,(喵喵喵木马)