题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
#include <cctype> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string input; vector<string> outWords; getline(cin, input); int pos = 0; while(pos<input.length() && (!isalpha(input[pos]))) ++pos; for(int i=pos; i<input.length(); ++i) { if(!isalpha(input[i])) { outWords.push_back(input.substr(pos, i-pos)); pos = i; while(pos<input.length() && (!isalpha(input[pos]))) ++pos; } } outWords.push_back(input.substr(pos, input.length()-pos)); for(int i=outWords.size()-1; i>=0; --i) { if(i) cout << outWords[i] << " "; else cout << outWords[0]; } return 0; } // 64 位输出请用 printf("%lld")