1 条题解
-
0
练69.2 单词倒排
解题思路
以空格读取全部单词保存数组,再逆序输出。题目只倒排词序,不翻转词内字符。
正确性说明
依题目给出的对应关系逐项处理,位置与原输入保持明确对应。每一步完成当前项的全部贡献,结束后得到所有位置或字符的目标结果。
易错点
词间保持一个空格。
复杂度
时间O(L),空间O(L)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); vector<string> words; string s; while (cin >> s) words.push_back(s); for (int i = int(words.size()) - 1; i >= 0; i--) { if (i != int(words.size()) - 1) cout << " "; cout << words[i]; } cout << "\n"; return 0; }
- 1