字符串

122 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第21天,点击查看活动详情

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

题目描述

你需要开发一款文字处理软件。最开始时输入一个字符串作为初始文档。可以认为文档开头是第 00 个字符。需要支持以下操作:

  • 1 str:后接插入,在文档后面插入字符串 str\texttt{str},并输出文档的字符串。

  • 2 a b:截取文档部分,只保留文档中从第 aa 个字符起 bb 个字符,并输出文档的字符串。

  • 3 a str:插入片段,在文档中第 aa 个字符前面插入字符串 str\texttt{str},并输出文档的字符串。

  • 4 str:查找子串,查找字符串 str\texttt{str} 在文档中最先的位置并输出;如果找不到输出 1-1

为了简化问题,规定初始的文档和每次操作中的 str\texttt{str} 都不含有空格或换行。最多会有 qq 次操作。

输入格式

第一行输入一个正整数 qq,表示操作次数。

第二行输入一个字符串 str\texttt{str},表示最开始的字符串。

第三行开始,往下 qq 行,每行表示一个操作,操作如题目描述所示。

输出格式

一共输出 nn 行。

对于每个操作 1,2,31,2,3,根据操作的要求输出一个字符串。

对于操作 44,根据操作的要求输出一个整数。

样例 #1

样例输入 #1

4
ILove
1 Luogu
2 5 5
3 3 guGugu
4 gu

样例输出 #1

ILoveLuogu
Luogu
LuoguGugugu
3

提示

数据保证,1q1001 \leq q\le 100,开始的字符串长度 100\leq 100

分析

这题之前就写过,但是wa了,时隔两个月,重新做这题,感觉是对字符串的一个巩固和理解把,qaq。

代码

#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
#include <unordered_map>
#define ll long long
#define lowbit(x) x&(-x) 
#define vc vector<ll>
#define mp map<ll,ll>
using namespace std;
const int N=1010000;
int n;
string s;
int main(){
	int t;
	cin>>t;
	cin>>s; 
	while(t--){
		int op;
		cin>>op;
		string ss;
		if(op==1){
			string t;
			cin>>t;
			s.append(t);
			cout<<s<<endl;
		}
		if(op==2){
			int a,b;
			cin>>a>>b;
			s=s.substr(a,b);
			cout<<s<<endl;
		}
		if(op==3){
			int a;
			string t;
			cin>>a>>t;
			s=s.insert(a,t);
			cout<<s<<endl;
		}
		if(op==4){
			string t;
			cin>>t;
			int m=int(s.find(t));
			cout<<m<<endl;
		}
	}
}