题目
![]()
解法1
点击查看代码
#include <iostream>
#include <vector>
#include<unordered_map>
#include <algorithm>
using namespace std;
int main(){
int n,k; scanf("%d %d",&n,&k);
unordered_map<string,vector<int> > map;
while(k--){
int idx,stdnum; scanf("%d %d",&idx,&stdnum);
while(stdnum--){
string name; cin>>name;
if(map.find(name)!=map.end()){ //学生已存在,只用加课程即可
map[name].push_back(idx);
}else{ // 学生不存在,新建学生
vector<int> classlist;
classlist.push_back(idx);
map.insert({name,classlist});
}
}
}
while(n--){
string name; cin>>name;
printf("%s %d",name.c_str(),map[name].size());
sort(map[name].begin(),map[name].end());
for(int i =0;i<int(map[name].size());i++){
printf(" %d",map[name][i]);
}
printf("\n");
}
return 0;
}