题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
#include <iostream> #include <string> #include <cctype> // 包含 isalpha 函数 #include <sstream> #include <vector> using std::cin; using std::cout; using std::endl; using std::string; using std::istringstream; using std::vector; void deal_other_character(string& rhs) { //将其它字符转换为空格 auto it = rhs.begin(); for (; it != rhs.end(); it++) { if (!isalpha(*it)) { *it = ' '; } } } void test() { string str1, word; getline(cin, str1); deal_other_character(str1); //本题最重要的是分割一个句子为单独的单词(string) istringstream iss(str1); vector<string> vec; while (iss >> word) { vec.push_back(word); } for (int i = vec.size() - 1; i >= 0; i--) { cout << vec[i] << " "; } } int main(int argc, char* argv[]) { test(); return 0; } // 64 位输出请用 printf("%lld")