题意:第一行给出课表有的人数N,总课程数K。
下一行就是K组,每一组包含学生编号,该学生的课程总数,最后给出十一个课程,求每一个课程的人数及编号。
题解:建立哈希数组,利用map进行以ID为下标遍历。
详细代码样例:
#include <bits/stdc++.h>
using namespace std;
int main(){
int N,K;
unordered_map <string,set<int>> MAP;//下标为string 用课程为下标查询学生ID和总数
cin>>N>>K;
while(K--){//数据输入
int i,j;
cin>>i>>j;
while(j--){
string c;
cin>>c;
MAP[c].insert(i);//以c为下标插入i
}
}
while(N--){
string temp;
cin>>temp;
cout<<temp<<' '<<MAP[temp].size();//在temp这个课程的下标里面含有多少学生
for(auto it:MAP[temp])
cout<<' '<<it;//输出temp下标的学生ID
cout<<endl;
}
}
本题比较简单哈,主要利用哈希进行一个整体排序。