P5734 【深基6.例6】文字处理软件[c++版]
生活随笔
收集整理的這篇文章主要介紹了
P5734 【深基6.例6】文字处理软件[c++版]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接
https://www.luogu.com.cn/problem/P5734
傳送門
題目描述
你需要開發一款文字處理軟件。最開始時輸入一個字符串(不超過 100 個字符)作為初始文檔。可以認為文檔開頭是第 0 個字符。需要支持以下操作:
1 str:后接插入,在文檔后面插入字符串 str,并輸出文檔的字符串。
2 a b:截取文檔部分,只保留文檔中從第 a 個字符起 b 個字符,并輸出文檔的字符串。
3 a str:插入片段,在文檔中第 a 個字符前面插入字符串 str,并輸出文檔的字符串。
4 str:查找子串,查找字符串 str 在文檔中最先的位置并輸出;如果找不到輸出 -1。
為了簡化問題,規定初始的文檔和每次操作中的 str 都不含有空格或換行。最多會有 (q≤100) 次操作。
輸入格式
無
輸出格式
無
輸入輸出樣例
輸入 #1
4 ILove 1 Luogu 2 5 5 3 3 guGugu 4 gu輸出 #1
ILoveLuogu Luogu LuoguGugugu 3提示
自己寫函數或者直接使用STL!
本題用到的STL:
substr 生成子串,輸入位置和長度
insert 在字符串中插入字符串
find 查找字符串中某個字符串的位置并返回它的位置
代碼
#include<iostream> #include<string> using namespace std; int main(){int n;string str;cin >> n >> str;for(int i = 0;i <n;i++){int temp;cin >> temp;switch(temp){case 1:{string str1;cin >> str1;str = str + str1;//或者使用 str.append(str1);cout << str << endl;break;}case 2:{int a,b;cin >> a >> b;str = str.substr(a,b);//截取str中從a開始的b個字符cout << str << endl;break;}case 3:{int a;string str1;cin >> a >>str1;str.insert(a,str1);//在str的第a個位置插入str1cout << str <<endl;break;}case 4:{string str1;cin >> str1;int a = str.find(str1);//在str中尋找str1的位置cout << a << endl;}}}return 0; }總結
以上是生活随笔為你收集整理的P5734 【深基6.例6】文字处理软件[c++版]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哪位大侠清楚表面处理等级与表面粗糙度对涂
- 下一篇: 将linux安装在u盘lili usb,