当前位置:网站首页>3342:字符串操作 题解
3342:字符串操作 题解
2022-08-11 01:40:00 【wkh2021】
看到 CSDN 上这道题的题解都比较冗长,在这里给出精简代码,
这道题是一道不错的 string 类函数练手题,需要注意的细节不少
AC code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
#define a_i atoi(get().c_str())
string a[25],get();int n=0;
inline string add(){
string s1=get(),s2=get();
if(s1.size()>5||s2.size()>5)return s1+s2;
int a1=atoi(s1.c_str()),a2=atoi(s2.c_str());
string s3=to_string(a1),s4=to_string(a2);
if(s1==s3&&s2==s4)return to_string(a1+a2);
return s1+s2;
}
inline string copy(){
int x=a_i,pos=a_i,len=a_i;return a[x].substr(pos,len);}
inline string find(){
string s=get();int x=a_i;
return a[x].find(s)==string::npos?to_string((int)s.length()):to_string(a[x].find(s));
}
inline string rfind(){
string s=get();int x=a_i;
return a[x].rfind(s)==string::npos?to_string((int)s.length()):to_string(a[x].rfind(s));
}
inline void insert(){
string s=get();a[a_i].insert(a_i,s);}
inline void reset(){
string s=get();a[a_i]=s;}
inline void print(){
cout<<a[a_i]<<'\n';}
inline void printall(){
for(int i=1;i<=n;i++)cout<<a[i]<<'\n';}
inline string get(){
string s;cin>>s;
if(s=="add")return add();
else if(s=="copy")return copy();
else if(s=="find")return find();
else if(s=="rfind")return rfind();
else if(s=="insert")insert();
else if(s=="reset")reset();
else if(s=="print")print();
else if(s=="printall")printall();
else return s;return "";
}
signed main(){
cin>>n;for(int i=1;i<=n;i++)cin>>a[i];
while(get()!="over");
return 0;
}
边栏推荐
- Vim take on a window.
- 络达开发---UI定义+自定义按钮事件
- Ora - 00001 in violation of the only constraint
- Ambari Migrates Spark2 to Other Machines (Graphic and Text Tutorial)
- wincc如何实现远程监控1200PLC
- MySQL Basics [Part 1] | Database Overview and Data Preparation, Common Commands, Viewing Table Structure Steps
- 如何实现FPGA的可重复性设计
- 版本号大小的判断方法
- 数据的存储(下)——浮点型在内存中的存储
- Successfully resolved TypeError: can't multiply sequence by non-int of type 'float'
猜你喜欢
[GXYCTF2019]BabySQli
MySQL advanced query
WinForm(五)控件和它的成员
MySQL Basics [Part 1] | Database Overview and Data Preparation, Common Commands, Viewing Table Structure Steps
Lianshengde W801 series 6-Analyze the Bluetooth communication source code of W801 from the perspective of WeChat applet (indicate method)
导入数据包上传宝贝提示“类目不能为空”是什么原因,怎么解决?
Data Filters in ABP
[The method of calling the child page from the parent page of the iframe] Stepping on the pit: It is the key to use `[x]` when getting elements. You cannot use `.eq(x)`, otherwise it will not be obtai
Qt 中的隐式共享
【服务器数据恢复】raid5崩溃导致lvm信息和VXFS文件系统损坏的数据恢复案例
随机推荐
Sub-database sub-table ShardingSphere-JDBC notes arrangement
loop word
报考PMP需要做些什么准备?
14.cuBLAS开发指南中文版--cuBLAS中的Level-1函数nrm2()和rot()
一次简单的 JVM 调优,拿去写到简历里
Ambari Migrates Spark2 to Other Machines (Graphic and Text Tutorial)
微服务概念
【服务器数据恢复】raid5崩溃导致lvm信息和VXFS文件系统损坏的数据恢复案例
Lianshengde W801 series 5-WeChat applet and W801 Bluetooth communication routine (read notes)
MySQL indexes and transactions
软件测试面试题:性能测试工作?
2022英伟达显卡排名天梯图
HCIP-R&S By Wakin自用笔记(3)OSPF之引入外部路由、Forwarding-Address、汇总、特殊区域
两日总结九
Qt 中的隐式共享
【HFSS学习记录1】实例:宽带非对称多节定向耦合器设计
软件测试面试题:Web服务器指标指标?
#yyds Dry Goods Inventory#[Yugong Series] August 2022 Go Teaching Course 008-Integer of Data Types
软件测试面试题:验收测试包括哪三种类型?
21. Aliyun oss