2 条题解
-
0
#include <iostream> #include <string> #include <sstream> #include <set> using namespace std; int main() { string line; getline(cin, line); // 读取整行输入 istringstream iss(line); // 创建字符串流 set<string> unique_words; // 利用set自动去重和排序 string word; while (iss >> word) { // 自动处理多个空格分割 unique_words.insert(word); // 插入单词(自动去重) } for (const auto& w : unique_words) { cout << w << endl; // 按字典序输出 } return 0; } 代码解析: 输入处理: 使用getline读取整行输入,确保捕获所有空格 istringstream自动处理多个空格分割,简化单词提取 核心数据结构: set<string> 实现自动: 去重(保证唯一性) 排序(默认字典序) 时间复杂度: 插入操作:O(n log n),适用于n ≤ 100的规模 遍历输出:O(n)``` ```
信息
- ID
- 1350
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 7
- 标签
- (无)
- 递交数
- 14
- 已通过
- 11
- 上传者