这题卡时间。。。直接用
map<string,set<int> > stus;or
map<string,vector<int> > stus;(一个学生应该也选不了多少课,set应该也多花不了多少时间)最后一个2分点怎么也过不去
然后
名字格式清晰,题目中格外强调了一下。。。所以间接提示用hash
A student name consists of 3 capital English letters plus a one-digit number.
附上代码:
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
const int N = 26*26*26*10;
vector<int> stus[N];
int Hash(char* name){
return (name[0]-'A')*26*26*10+(name[1]-'A')*26*10+(name[2]-'A')*10+(name[3]-'0');
}
int main(){
int n, k;
cin>>n>>k;
for(int i = 0; i < k; i++){
int num, couse;
scanf("%d%d",&couse,&num);
for(int j = 0; j < num; j++){
char* name = new char[4];
scanf("%s",name);
stus[Hash(name)].push_back(couse);
}
}
for(int i = 0; i < n; i++){
char* qname = new char[4];
scanf("%s",qname);
int temp = Hash(qname);
int count = stus[temp].size();
sort(stus[temp].begin(),stus[temp].end());
printf("%s %d",qname,count);
for(int j = 0; j < count; j++){
printf(" %d",stus[temp][j]);
}
printf("\n");
}
return 0;
}
本文介绍了一种利用Hash技术优化课程选择系统的实现方法。通过将学生姓名转换为整数索引,有效地解决了课程信息查询效率的问题。文章分享了具体的C++实现代码,并讨论了时间复杂度及优化技巧。
2170

被折叠的 条评论
为什么被折叠?



