假设全校有最多40000名学生和最多2500门课程。现给出每个学生的选课清单,要求输出每门课的选课学生名单。
输入格式:
输入的第一行是两个正整数:N(≤40000),为全校学生总数;K(≤2500),为总课程数。此后N行,每行包括一个学生姓名(3个大写英文字母+1位数字)、一个正整数C(≤20)代表该生所选的课程门数、随后是C个课程编号。简单起见,课程从1到K编号。
输出格式:
顺序输出课程1到K的选课学生名单。格式为:对每一门课,首先在一行中输出课程编号和选课学生总数(之间用空格分隔),之后在第二行按字典序输出学生名单,每个学生名字占一行。
输入样例:
10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5
输出样例:
1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
总体的思路是这样的:用map的键来对应课程号,值来对应选该课程号的学生姓名(用vector的string类,具体事例如下:)
map对应的vector里面有很多string类,想通了就很好想了
#include <iostream>
#include <string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include <vector>
#include <map>
#include<queue>
#include<deque>
#include<cctype>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
using namespace std;
int main(){
int n,k,c;
string name;
map<int,vector<string>>mp;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++){
cin>>name;
scanf("%d",&c);
for(int j=0;j<c;j++){
int x;
scanf("%d",&x);
mp[x].push_back(name);//x对应课程号插入姓名
}
}
for(int i=1;i<=k;i++){
c=mp[i].size();
sort(mp[i].begin(),mp[i].end());//字母按字典顺序排序
printf("%d %d\n",i,c);
for(int j=0;j<c;j++){
printf("%s\n", mp[i][j].c_str());//输出姓名即可
}
}
return 0;
}