#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = 40010;
const int MAXC = 2510;
vector<int> Courses[MAXC];
char StuName[MAXN][5];
bool cmp(int a, int b){
//按照名字典序从小到大排序
return strcmp(StuName[a], StuName[b]) < 0;
}
int main(){
int N, K, SelectCourseNumber, CourseIndex;
scanf("%d %d", &N, &K); //学生人数及课程数
for(int i = 0; i < N; i++){
scanf("%s %d", StuName[i], &SelectCourseNumber); //学生姓名及选课数
for(int j = 0; j < SelectCourseNumber; j++){
scanf("%d", &CourseIndex); //选择的课程编号
Courses[CourseIndex].push_back(i); //将学生 i 加入第 CourseIndex门课中
}
}
for(int i = 1; i <= K; i++){
printf("%d %d\n", i, Courses[i].size()); //第 i 门课的学生人数
sort(Courses[i].begin(), Courses[i].end(), cmp); //对第 i 门课的学生排序
for(int j =0 ; j< Courses[i].size(); j++){
printf("%s\n", StuName[Courses[i][j]]); //输出学生姓名
}
}
return 0;
}
原文地址:https://www.cnblogs.com/zjsaipplp/p/10425227.html
时间: 2024-11-02 11:33:55