题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[]){
string str;
getline(cin, str);
vector<bool> isAlpha(str.size(), false);
for(int i = 0; i < str.size(); ++i){
if(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z'){
isAlpha[i] = true;
}
}
string res;
for(int i = str.size() - 1; i >= 0; --i){
int end = i;
while(i >= 0 && isAlpha[i]){
--i;
}
if(end > i){
res = res + str.substr(i+1, end-i) + " ";
}
}
cout << res << endl;
return 0;
}