当前位置:网站首页>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;
}
边栏推荐
- Ambari Migrates Spark2 to Other Machines (Graphic and Text Tutorial)
- 软件测试面试题:什么是Negative测试?
- WinForm (5) control and its members
- Data Filters in ABP
- 颠覆性创新招商,链动2+1是个怎么样的制度模式?
- MySQL索引与事务
- MySQL Basics [Part 1] | Database Overview and Data Preparation, Common Commands, Viewing Table Structure Steps
- Lianshengde W801 series 5-WeChat applet and W801 Bluetooth communication routine (read notes)
- dump_stack ()
- Section 4-6 of the first week of the second lesson: Appreciation of medical prognosis cases + homework analysis
猜你喜欢
随机推荐
The concept of services
ora-00001违反唯一约束
Still using Xshell?You are out, recommend a more modern terminal connection tool, easy to use!
MySQL advanced query
R language multiple linear regression, ARIMA analysis of the impact of different candidates in the United States on the economic GDP time series
C# WebBrower1控件可编辑模式保存时会提示“该文档已被修改,是否保存修改结果”
Qt 中的隐式共享
报考PMP需要做些什么准备?
std::format格式化自定义类型
14.cuBLAS开发指南中文版--cuBLAS中的Level-1函数nrm2()和rot()
Two-dimensional array combat project -------- "Minesweeper Game"
如何防止离职员工把企业文件拷贝带走?法律+技术,4步走
SyntaxError: invalid syntax
postgresql parameter meaning
Summary of DDL routine operations in MySQL
The latest domestic power supply manufacturers and pin-to-pin replacement manuals for specific models are released
Shengxin experiment record (part2)--tf.reduce_sum() usage introduction
【HFSS学习记录1】实例:宽带非对称多节定向耦合器设计
21、阿里云oss
数据的存储(下)——浮点型在内存中的存储









