题解
【深基17.例6】学籍管理
1 条题解
-
0
1. 解题思路
用姓名到成绩的map维护学籍,分别实现赋值、查找、删除和数量查询。
2. 坑点和注意事项
q≤10^5,姓名≤20且区分大小写,成绩小于2^31。查询用find避免无意新增学生;重复插入更新成绩,不增加人数。字符串键无法直接用普通数组下标。
3. 复杂度
- 时间复杂度:O(qL log q),L≤20。
- 空间复杂度:O(qL)。
/* 思路:用姓名到成绩的map维护学籍,分别实现赋值、查找、删除和数量查询。 关键规则与坑点:q≤10^5,姓名≤20且区分大小写,成绩小于2^31。查询用find避免无意新增学生;重复插入更新成绩,不增加人数。字符串键无法直接用普通数组下标。 时间复杂度:O(qL log q),L≤20。空间复杂度:O(qL)。 */ #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int q; cin >> q; map<string, int> a; while (q--) { int op; cin >> op; if (op == 4) { cout << a.size() << '\n'; continue; } string name; cin >> name; if (op == 1) { int score; cin >> score; a[name] = score; cout << "OK\n"; } else if (op == 2) { map<string, int>::iterator it = a.find(name); if (it == a.end()) cout << "Not found\n"; else cout << it->second << '\n'; } else cout << (a.erase(name) ? "Deleted successfully" : "Not found") << '\n'; } }
- 1