当前位置:网站首页>Hj13 sentence reverse order
Hj13 sentence reverse order
2022-07-21 08:35:00 【fareast_ mzh】
HJ13 The sentence is in reverse order
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
class Solution {
private:
static bool charInStr(char c, const char *sep) {
bool b = false;
for (int i = 0; sep[i] != '\0'; i++) {
if (c == sep[i]) {
b = true;
break;
}
}
return b;
}
std::vector<std::string> explode(const char *sep, std::string s) {
std::vector<std::string> vs;
int i = 0;
for (; i <s.size(); i++) {
if (false == charInStr(s[i], sep)) {
break;
}
}
int j = i;
for (; i < s.size(); i++) {
if (charInStr(s[i], sep)) {
vs.push_back(s.substr(j, i-j));
j = i+1;
}
}
if (j < i) {
vs.push_back(s.substr(j, i-j));
}
return vs;
}
static void reverse(std::vector<std::string> &vs) {
std::string word;
for (int i = 0, j = vs.size()-1; i < j; i++, j--) {
word = vs[i];
vs[i] = vs[j];
vs[j] = word;
}
}
static char *join(const std::vector<std::string>& words, std::string delim) {
size_t sz = 0;
std::vector<std::string>::const_iterator it;
for (it = words.begin(); it != words.end(); ++it) {
sz += it->size();
}
sz += delim.size() * (words.size()-1);
sz += 1;
char *ans = (char *)malloc(sizeof(char) * sz);
char *p = ans;
it = words.begin();
::strncpy(ans, it->c_str(), it->size());
p += it->size();
++it;
for (; it != words.end(); ++it) {
::strncpy(p, delim.c_str(), delim.size());
p += delim.size();
::strncpy(p, it->c_str(), it->size());
p += it->size();
}
return ans;
}
public:
char *reverseWords(std::string line) {
std::vector<std::string> words = explode(" \t\r\n", std::move(line));
reverse(words);
// for (auto w : words) {
// std::cout << w << std::endl;
// }
return join(words, " ");
}
};
int main(){
char line[1000] = {'\0'};
std::cin.getline(line, 1000);
// strcpy(line, "I am a boy");
Solution s;
char *ans = s.reverseWords(std::string(line));
std::cout << ans << std::endl;
free(ans);
return 0;
}
边栏推荐
- CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
- mybats中xml参数类型的设置
- How many months did you write your first SCI?
- 进程间通信-共享内存shmat
- Control the operation of the test module through the panel in canoe (Advanced)
- ProSci 抗CD22抗体Epratuzum28流式细胞术展示
- 2.rsync
- HJ76 尼科彻斯定理
- The method of using postman to test the interface and send the request with cookie
- Characteristics and performance parameters of the sensor
猜你喜欢
随机推荐
FreeRTOS个人笔记-FreeRTOSConfig.h
限制input框中的输入类型及长度
业务出海,灵感乍现前要先「把手弄脏」
3. Rsync backup case
Selenium grid installation
点云格式读取和保存
Leetcode 322 coin change, 找零钱的最小张数
SAP Spartacus 服务器端渲染的三种可能情形
Google lance une autre bataille pour construire des noyaux, rejoignant Intel, un vétéran de 17 ans
在CANoe中通过Panel面板控制Test Module 运行(初级)
Worthington细胞色素 C 消化研究丨羧肽酶 B方案
The method of using postman to test the interface and send the request with cookie
4. Storage NFS
Examples illustrate the division basis of code segment (.Text), data segment (.Data), BSS segment, read-only data segment (.Rodata) and stack
Google 为造芯再掀“抢人大战”,英特尔 17 年老将加入
【c ++ primer 笔记】第6章 函数
CAPL脚本中关于相对路径/绝对路径操作的几个傻傻分不清的内置函数
MySQL基础篇(概念、常用指令)
Web漏洞安全-失效访问权限控制
rsync 结合 inotify 实现文件实时同步(一)