7-47 打印选课学生名单 (25 分)

假设全校有最多40000名学生和最多2500门课程。现给出每个学生的选课清单,要求输出每门课的选课学生名单。

输入格式:
输入的第一行是两个正整数:N(≤40000),为全校学生总数;K(≤2500),为总课程数。此后N行,每行包括一个学生姓名(3个大写英文字母+1位数字)、一个正整数C(≤20)代表该生所选的课程门数、随后是C个课程编号。简单起见,课程从1到K编号。

输出格式:
顺序输出课程1到K的选课学生名单。格式为:对每一门课,首先在一行中输出课程编号和选课学生总数(之间用空格分隔),之后在第二行按字典序输出学生名单,每个学生名字占一行。

输入样例:
10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5
输出样例:
1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1

解答:string+cin+cout会超时

#include <iostream>
#include <map>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cstdio>

using namespace std;

vector<vector<char*>> menu;

bool cmp(const void *a,const void *b){
	int res=strcmp((char*)a,(char*)b);
	if(res<0){
		return true;
	}
	return false;
}

int main(){
	int n,k,subjectNUM,id;
	scanf("%d %d",&n,&k);
	for(int i=0; i<n; i++){
		vector<char*> temp;
		menu.insert(menu.begin()+i,temp);
	}
	for(int i=0; i<n; i++){
		char *name=new char[5];
		scanf("%s %d",name,&subjectNUM);
		for(int j=0; j<subjectNUM; j++){
			scanf("%d",&id);
			id--;
			(menu.at(id)).push_back(name);
		}
	}
	int len;
	for(int i=0; i<k; i++){
		len=(menu.at(i)).size();
		printf("%d %d\n",i+1,len);
		sort((menu.at(i)).begin(),(menu.at(i)).end(),cmp);
		for(int j=0; j<len; j++){
			printf("%s\n",(menu.at(i)).at(j));
		}
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值