1047. Student List for Course

give the hash key to every student,then sort the students in increasing order of the students name.

to every course,traverse the list of student who select the course,his own hash key will exists.

traverse every students,if he has the hash key(select the course) then must print his name.

as we had sorted the students,so the answer will be in increasing order.

learn to use hash theory,about this problem,time is so value.

// 1047. Student List for Course.cpp: 主项目文件。

#include "stdafx.h"
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using std::sort;
using std::vector;

const int Cmax=2503;
const int Smax=40003;
//the hash key every student reflects,begin from 0
bool hash[Smax];
//construct the hash key
typedef struct Stu{
	char name[5];
	int id;
}Stu;
Stu stu[Smax];
vector<int> courseList[Cmax];

bool cmp(Stu m1,Stu m2){
	return strcmp(m1.name,m2.name)<0;
}

int main()
{
	int students,course;
	scanf("%d%d",&students,&course);
	for(int i=0;i<students;i++){
		int num;
		scanf("%s%d",stu[i].name,&num);
		stu[i].id=i;
		while(num--){
			int temp;
			scanf("%d",&temp);
			courseList[temp].push_back(i);
		}
	}
	sort(stu,stu+students,cmp);
	memset(hash,0,sizeof(hash));
	for(int i=1;i<=course;i++){
		printf("%d %d\n",i,courseList[i].size());
		//if the student exists,give the hash key 
		for(vector<int>::iterator ite=courseList[i].begin();ite!=courseList[i].end();ite++)
			hash[*ite]=true;
		for(int j=0;j<students;j++){
			if(hash[stu[j].id])//hash key exists
				printf("%s\n",stu[j].name);
		}
		for(vector<int>::iterator ite=courseList[i].begin();ite!=courseList[i].end();ite++)
			hash[*ite]=false;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值