思路:
1. 学生姓名用 【字符串hash】映射为int型数字作为下标;
2. 用vector<int> 型的数组 存放每个学生的选择课程的课程编号;
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 40010;
const int M = 26 * 26 * 26 * 10;
vector<int> selectCourse[M];
int getID(char name[]) {
int id = 0;
for (int i = 0; i < 3; i++) {
id = id * 26 + (name[i] - 'A');
}
id = id * 10 + (name[3] - '0');
return id;
}
int main() {
int n, k;
char name[5];
scanf("%d%d", &n, &k);
for (int i = 0; i < k; i++) {
int course, num;
scanf("%d%d", &course, &num);
for (int j = 0; j < num; j++) {
scanf("%s", name);
int id = getID(name);
selectCourse[id].push_back(course);
}
}
for (int i = 0; i < n; i++) {
scanf("%s", name);
int id = getID(name);
printf("%s %d", name, selectCourse[id].size());
for (int j = 0; j < selectCourse[id].size(); j++) {
printf(" %d", selectCourse[id][j]);
}
printf("\n");
}
return 0;
}