1039. Course List for Student (25)

本文介绍了一种利用Hash技术优化课程选择系统的实现方法。通过将学生姓名转换为整数索引,有效地解决了课程信息查询效率的问题。文章分享了具体的C++实现代码,并讨论了时间复杂度及优化技巧。

这题卡时间。。。直接用

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;
} 


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值