[C++] 36855 -- 单词倒排


例题描述

对字符串中的所有单词进行倒排。

说明:

  1. 每个单词是以26个大写或小写英文字母构成;

  2. 非构成单词的字符均视为单词间隔符;

  3. 要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;

  4. 每个单词最长20个字母;

  • 输入描述:
    输入一行以空格来分隔的句子。

  • 输出描述:
    输出句子的逆序。

示例1:

  • 输入:
    I am a student
  • 输出:
    student a am I

以前的思路不能解决该问题

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() {
	string str;
	while (getline(cin, str)) {
		std::reverse(str.begin(), str.end());
		for (int i = 0; i < (int)str.size(); ++i) {
			if (isspace(str[i])) continue;
			int index = i++;
			while (!isspace(str[i]) && i < (int)str.size()) ++i;
			int j = i - 1;
			while (index < j) {
				char tmp = str[index];
				str[index] = str[j];
				str[j] = tmp;
				index++;
				j--;
			}
		}
		cout << str << endl;
	}
	return 0;
}

运行结果

答案错误:您提交的程序没有通过所有的测试用例
case通过率为90.00%

用例:
$bo*y gi!r#l #

对应输出应该为:

l r gi y bo

你的输出为:

# gi!r#l $bo*y

因为本题存在分隔符的问题,所以需要换更换思路。


思路一

这个问题是包含了字符串常见操作的 切分合并, 这个问题对单词分割符不设限制, 只要非字母, 都需要当成分割符, 并且分割符的数目不受限制。

整体思路为:

  1. 先切分(切分前先对分割符做处理, 统一分割符) , 此时可以使用 stringstream 进行切分。
  2. 再合并(对切分结果逆序合并) 直接字符串拼接合并即可。

代码实现

#include<iostream>
#include<sstream>
#include<string>
#include<vector>

using namespace std;

void Solution(vector<string>& res, string& str, char delim = ' ') {
	// 对字符串进行切分, 借助 stringstream 完成. 默认分割符是空格.
	// stringstream 不光能完成切分, 还能完成字符串和数字之间的转换.
	stringstream ss;
	ss << str;
	string tmp;
	while (ss >> tmp) {
		res.push_back(tmp);
	}
}
int main() {
	string str;
	while (getline(cin, str)) {
		vector<string> res;
		for (int i = 0; i < str.size(); i++){
			// 1. 先对分割符进行处理. 如果发现分割符, 就统一处理成空格.
			if (!isalnum(str[i]))
				str[i] = ' ';
		}
		
		// 2. 对字符串进行切分
		Solution(res, str);
		cout << res[res.size() - 1];
		for (int i = res.size() - 2; i >= 0; i--)
			cout << " " << res[i];
		cout << endl;
	}
	return 0;
}


思路二

从前往后扫描字符串,遇到是单词就放入vector,最后反向输出即可。

代码实现

#include<iostream>
#include<vector>
#include<string>

using namespace std;

int main(){
	string str;
	while (getline(cin, str)){
		vector<string>svec;
		svec.clear();
		string temp = "";
		for (int i = 0; i < str.size(); ++i){
			if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z')
				temp += str[i];
			else{
				if (temp.size() > 0){
					svec.push_back(temp);
					temp = "";
				}
			}
		}
		if (temp.size() > 0)
			svec.push_back(temp);
		for (int i = svec.size() - 1; i > 0; --i)
			cout << svec[i] << ' ';
		cout << svec[0] << endl;
	}
	return 0;
}

链接:https://www.nowcoder.com/questionTerminal/81544a4989df4109b33c2d65037c5836

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

giturtle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值