ACGO巅峰赛#22+题解
2025-06-23 13:58:25
发布于:河北
27阅读
0回复
0点赞
解题思路
- 数据存储:将所有计划参与学生的姓名按学号顺序存入数组,学号 对应数组下标 。
- 快速查找:使用哈希集合存储实际出席学生的姓名,实现 时间复杂度的存在性检查。
- 遍历判断:按学号顺序遍历数组,若某学生姓名不在集合中,则输出该姓名。
代码解释
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<string> students(n);
for (int i = 0; i < n; ++i) {
cin >> students[i]; // 按学号顺序存储,students[0]是学号1
}
unordered_set<string> attended;
for (int i = 0; i < m; ++i) {
string name;
cin >> name;
attended.insert(name); // 存储出席姓名
}
for (const string& name : students) {
if (attended.find(name) == attended.end()) { // 检查是否未出席
cout << name << '\n'; // 按学号顺序输出未出席者
}
}
return 0;
}
这里空空如也
有帮助,赞一个