当前位置:网站首页>每日一题(02):倒置字符串
每日一题(02):倒置字符串
2022-07-27 16:29:00 【刘敬一如】

学习启示:
1.首先这是一道操作题。初态和末态已知。所以要考虑 “模板的映射”。
2.基本功:熟练的双层循环操作+写初末终止条件
3.熟能生巧:auto关键字的应用,不断地自动赋值+(C++STL库的应用)
4.模板映射---》数据可以分为字母,数字,空格.....等。可以改变数据范围(限定字母用ACII来判断)倒置格式的不同设定。
5.”思路问题“---》有点递归的思想。如果没有分析出倒两次等于自己的事实(a与b异或两次等于b,,,有的时候思路是抽象的,也依赖于当时的灵感),就不会想到将单词封装成一个再倒置,就可能会通过逐个遍历字母的方式取取反,会很混乱。边界判断越多,越用容易写错。循环一次,就代表可能带来O(n)复杂度,所以要灵活考虑简化。
6.STL库本质上是数据结构,处理字符串和链表等问题,借鉴思想和分析方式,配合循环和写判断条件的基本功。另一边还要借助STL库的遍历,简化步骤。
即使边界多,一步步写,也不易出错
伪代码
1.输入输出格式,以头文件,完成变量初始化
2.整体逆置
3.单词逆置
检索每个单词,一个单词逆置一次,
每个单词逆置条件: 右边界(s.end)遇到空格
4.处理边界,字符串以/0结尾。标志着检索和逆置结束#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
getline(cin, s);
//翻转整个句子
reverse(s.begin(), s.end());
//翻转单词
auto start = s.begin();
while (start != s.end())
{
auto end = start;
while (start != s.end() && *end != ' ')
end++;
reverse(start, end);
if (end != s.end())
start = end + 1;
else
start = end;
}
cout<< s << endl;
return 0;
}受限于知识,只是对当下的基础学习做一下总结,思路和知识上面尚且不足,如有错误欢迎指正!
边栏推荐
- 功率单位(power control)
- 进行接口测试时,连接数据库,对数据源进行备份、还原、验证操作
- Browser rendering principle analysis suggestions collection
- PHP string operation
- Summary of "performance test" of special test
- Mongodb learning notes (1) - install mongodb and its related configurations
- Learning notes of Microcomputer Principles - common addressing methods
- Blog Garden beautification tutorial
- Unity显示Kinect捕获的镜头
- Analysis of Eureka server
猜你喜欢

新系统安装MySQL+SQLyog

Mongodb learning notes (1) - install mongodb and its related configurations

一篇让你掌握线程和线程池,还解决了线程安全问题,确定不看看?

express

IDEA成功连接Database但不显示表怎么办

Definition of graph traversal and depth first search and breadth first search (2)

JMeter interface automation - how to solve the content type conflict of request headers

浅谈基本的网络基本故障和问题排查

阿里云视频点播服务的开通和使用

Subscription and use of Alibaba cloud video on demand service
随机推荐
IDEA成功连接Database但不显示表怎么办
Nodejs template engine EJS
2022 Ningde Vocational College Teachers' practical teaching ability improvement training - network construction and management
Study notes of Microcomputer Principles - general integer instructions and Applications
换行问题双保险
asp. Net experience
Sentinel1.8.4 persistent Nacos configuration
Unity shows Kinect captured shots
Useful resources for ns2
v-if,v-else,v-for
Browser rendering principle analysis suggestions collection
What if idea successfully connects to the database without displaying the table
101. (cesium chapter) cesium particle system - snow
用Matlab生成适用于期刊及会议的图形- plot
Unity学习笔记(刚体-物理-碰撞器-触发器)
The understanding of string in C.
WSN Journal indexed by SCI(转)
Unity学习笔记(实现传送带)
电磁场学习笔记-矢量分析和场论基础
Word 2007+ tips