PAT:1047. Student List for Course (25) AC
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX=40010;
int n,k; //n个人,k门课
char name[MAX][5]; //存n个人的名字
vector<int> course[MAX]; //记录每个课程选的学生
bool cmp(int a,int b)
{
return strcmp(name[a],name[b])<0; //按字典序排序
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1 ; i<=n ; ++i)
{
scanf("%s",name[i]);
int k=0;
scanf("%d",&k);
for(int j=1 ; j<=k ; ++j)
{
int c;
scanf("%d",&c);
course[c].push_back(i);
}
}
for(int i=1 ; i<=k ; ++i)
{
printf("%d %d\n",i,course[i].size());
sort(course[i].begin(),course[i].end(),cmp);
for(int j=0 ; j<course[i].size() ; ++j)
printf("%s\n",name[course[i][j]]);
}
return 0;
}