解法一:Map+Set
用 Map 和 Set 存储每个学生的选课信息, STL 的 Set 是有序的。
#include <bits/stdc++.h>using namespace std;// (name, set<course index>)map<string, set<int>> stuMap;int main() {ios::sync_with_stdio(false);cin.tie(0);int N, K;cin >> N >> K;int index, num;string stuName;for (int i = 0; i < K; ++i) {cin >> index >> num;for (int j = 0; j < num; ++j) {cin >> stuName;auto res = stuMap.find(stuName);if (res == stuMap.end()) {stuMap.emplace(stuName, set<int>{index});} else {res->second.insert(index);}}}for (int i = 0; i < N; ++i) {cin >> stuName;cout << stuName << " ";auto res = stuMap.find(stuName);if (res == stuMap.end()) {cout << "0\n";} else {cout << res->second.size();for (auto e:res->second) {cout << " " << e;}cout << '\n';}}}
