题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
#include <iostream> #include <string> #include <vector> using namespace std; int main() { string s; getline(cin, s); vector<string> ans; bool isWord = false; string w; for (int i = s.size() - 1; i >= 0; i--) { if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) { w = s[i] + w; } else { if (w.size() != 0) { ans.push_back(w); w.clear(); } } } if (w.size() != 0) { ans.push_back(w); w.clear(); } for (auto i : ans) { cout << i << ' '; } } // 64 位输出请用 printf("%lld")