题解 | #单词倒排#
单词倒排
http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string str = "";
getline(cin, str);
str += " ";
vector<string> vec;
string tmp = "";
for(char c : str){
if(!isalpha(c)){
if(!tmp.empty()){
vec.push_back(tmp);
tmp.clear();
}
}
else{
tmp += c;
}
}
reverse(vec.begin(), vec.end());
string res = "";
for(int i = 0; i < vec.size(); i++){
//cout << vec[i]<<endl;
res += vec[i];
res += " ";
}
res.pop_back();
cout << res << endl;
return 0;
}
华为题库题解 文章被收录于专栏
牛客华为题库的题解