当前位置:网站首页>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;
}
边栏推荐
- [Server data recovery] Data recovery case of lvm information and VXFS file system corruption caused by raid5 crash
- Please talk about for...in and for...of in JS (below)
- 想进阿里?先来搞懂一下分布式事务
- 安装dlib库
- SyntaxError: invalid syntax
- QT+VTK+PCL拟合圆柱并计算起始点、中止点
- MySQL advanced query
- 惨遭面试官吊打高并发系统设计,回来学习 2400 小时后成功复仇
- 软件测试面试题:I P协议、RARP协议、ICMP协议与ARP协议的功能是什么?
- How to convert url to obj or obj to url
猜你喜欢
SQL语句--获取数据库表信息,表名、列名、描述注释等
【服务器数据恢复】raid5崩溃导致lvm信息和VXFS文件系统损坏的数据恢复案例
【开源】壁纸软件,给自己电脑设计专属特效
Use mysql statement to operate data table (table)
apache+PHP+MySQL+word press, page error when installing word press?
【iframe父页面调用子页面的方法】踩坑:获取元素的时候需要用 `[x]`是关键,不能用`.eq(x)`否则获取不到。
FPGA learning column (xinlinx) serial communication -
R language multiple linear regression, ARIMA analysis of the impact of different candidates in the United States on the economic GDP time series
#yyds Dry Goods Inventory#[Yugong Series] August 2022 Go Teaching Course 008-Integer of Data Types
QT+VTK+PCL拟合圆柱并计算起始点、中止点
随机推荐
软件测试面试题:软件测试的过程的V模型,说出它的缺点?
leetcode 739. Daily Temperatures 每日温度(中等)
[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
进程间通信方式(1)无名管道(全CSDN最用心的博主)
本周四晚19:00知识赋能第六期第5课丨OpenHarmony WiFi子系统
nvidia-smi详解
21、阿里云oss
loop word
The latest domestic power supply manufacturers and pin-to-pin replacement manuals for specific models are released
OpenWrt之opkg详解
R language multiple linear regression, ARIMA analysis of the impact of different candidates in the United States on the economic GDP time series
MySQL进阶查询
install dlib library
std::format格式化自定义类型
阿里的数据同步神器——Canal
分库分表ShardingSphere-JDBC笔记整理
HCIP-R&S By Wakin自用笔记(3)OSPF之引入外部路由、Forwarding-Address、汇总、特殊区域
深度解析:什么是太爱速M抢单模式?
[Server data recovery] Data recovery case of lvm information and VXFS file system corruption caused by raid5 crash
Successfully resolved raise TypeError('Unexpected feature_names type')TypeError: Unexpected feature_names type