版本比较
题解:P5734 【深基6.例6】文字处理软件
正文
增加 31 行删除 34 行
12345678910111213141516171819202122232425262722 行未改动505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710840 行未改动149150151152153154本蒟蒻写的第一篇题解,有一些简陋,请见谅^V^[传送门](https://www.luogu.com.cn/problem/P5734)---# 思路:这道题所有的操作均可使用<string.h>头文件中的库函数来完成。需要使用这三个库函数:.substr() (截取字符串).insert() (从中间插入子串).find() (查找子串)同时,<string.h>也重载了运算符: +=+= 与 .append() 库函数的用法是一样的,都是在指定字符串的后面插入新的字符串。---# 实现:首先写出整体的代码框架,包括输入与判断 (~~谁都会~~):~~~ cpp#include<bits/stdc++.h>using namespace std;int main(){string s;22 行未改动}}return 0;}~~~这道题最难的部分在于 4 个操作:---操作一:我们只需要使用 string 重载后的 += 即可:~~~cppstring tmp;cin>>tmp;s+=tmp;cout<<s<<'\n';~~~---操作二:使用 substr 函数,截取字符串:~~~cppint a,b;cin>>a>>b;s=s.substr(a,b);cout<<s<<'\n';~~~---操作三:使用 insert 函数,直接往字符串 s 的 a 处插入 tmp 字符串:~~~cppint a;string tmp;cin>>a>>tmp;s.insert(a,tmp);cout<<s<<'\n';~~~操作四: find 函数可以直接查找子串,但要注意如果查找不到并不是直接返回 -1 ,而是返回一个常量: std::string::npos ,所以要加判断:~~~cppstring tmp;cin>>tmp;int a=s.find(tmp);if(a==(int)string::npos){cout<<-1<<'\n';}else{cout<<a<<'\n';}~~~最后附上全部代码:~~~cpp#include<bits/stdc++.h>using namespace std;int main(){string s;40 行未改动}}return 0;}~~~~~审核求过~~12345678910111213141516171819202122232425262722 行未改动505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510640 行未改动147148149150151[传送门](https://www.luogu.com.cn/problem/P5734)---# 思路:这道题所有的操作均可使用 `<string.h>` 头文件中的库函数来完成。需要使用这三个库函数:- `.substr()` (截取字符串)- `.insert()`(从中间插入子串)- . `find()` (查找子串)同时,`<string.h>` 也重载了运算符:`+=`。`+=` 与 `.append()` 库函数的用法是一样的,都是在指定字符串的后面插入新的字符串。# 实现:首先写出整体的代码框架,包括输入与判断(~~谁都会~~):```cpp#include<bits/stdc++.h>using namespace std;int main(){string s;22 行未改动}}return 0;}```这道题最难的部分在于 4 个操作:- 操作一:我们只需要使用 `string` 重载后的 $+=$ 即可:```cppstring tmp;cin>>tmp;s+=tmp;cout<<s<<'\n';```- 操作二:使用 `substr` 函数,截取字符串:```cppint a,b;cin>>a>>b;s=s.substr(a,b);cout<<s<<'\n';```- 操作三:使用 `insert` 函数,直接往字符串 $s$ 的 $a$ 处插入 $tmp$ 字符串:```cppint a;string tmp;cin>>a>>tmp;s.insert(a,tmp);cout<<s<<'\n';```- 操作四:`find` 函数可以直接查找子串,但要注意如果查找不到并不是直接返回 $-1$,而是返回一个常量:`std::string::npos`,所以要加判断:```cppstring tmp;cin>>tmp;int a=s.find(tmp);if(a==(int)string::npos){cout<<-1<<'\n';}else{cout<<a<<'\n';}```最后附上全部代码:```cpp#include<bits/stdc++.h>using namespace std;int main(){string s;40 行未改动}}return 0;}```