AcWing 1523. 学生课程列表
原题链接
中等
作者:
Value
,
2020-05-31 15:47:30
,
所有人可见
,
阅读 562
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
const int N = 4E4 + 10;
unordered_map<string, int> mp; // 建立索引
vector<vector<int>> course(N); // 学生的课表
int id = 0;
int main(){
int n, k;
cin >> n >> k;
int cno, num;
string name;
while(k -- ){
cin >> cno >> num;
while(num -- ){
cin >> name;
if(mp.find(name) == mp.end()) mp[name] = id ++ ;
course[mp[name]].push_back(cno);
}
}
while(n -- ){
cin >> name;
cout << name << " ";
if(mp.find(name) == mp.end()) cout << "0" << endl;
else{
int sno = mp[name];
cout << course[sno].size() << " ";
sort(course[sno].begin(), course[sno].end());
for(int i = 0; i < course[sno].size(); i ++ ){
cout << course[sno][i];
if(i == course[sno].size() - 1) cout << endl;
else cout << " ";
}
}
}
return 0;
}
其实可以直接
unordered_map<string, <vector<int>>