1047. Student List for Course (25)

Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=40000), the total number of students, and K (<=2500), the total number of courses. Then N lines follow, each contains a student's name (3 capital English letters plus a one-digit number), a positive number C (<=20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.

Output Specification:

For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students' names in alphabetical order. Each name occupies a line.


IDEA

1.超时问题:a.不能用string,需要用char [],排序sort比较时用strcmp;b.不能用cin,cout需要用scanf,printf。

二者如果有一个不满足都会造成超时。

2.struct Name{
char str[5];
};

vector< vector<Name> > vec(k);

vector中不能直接存char [],则先声明结构体Name,里面放char [] ,再将Name存进来就可是实现了


CODE

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector> 
#include<queue>
#include<algorithm>
#include<fstream>
using namespace std;
#define len 4
struct Name{
	char str[5];
};
int cmp(Name name1,Name name2){
//	for(int i=0;i<len;i++){
//		if(name1.str[i]==name2.str[i]){
//			continue;
//		}
//		return name1.str[i]<name2.str[i];
//	}
	return strcmp(name1.str,name2.str)<0;
}
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	#endif
	
	int n,k;
	//cin>>n>>k;
	scanf("%d %d",&n,&k);

	vector< vector<Name> > vec(k);
	for(int i=0;i<n;i++){
		//string name;
		//cin>>name;
		Name name;
		scanf("%s",name.str);
		int c;
		//cin>>c;
		scanf("%d",&c);
		for(int j=0;j<c;j++){
			int course;
			//cin>>course;
			scanf("%d",&course);
			vec[course-1].push_back(name);
		}
	}
	for(int i=0;i<k;i++){
		//cout<<i+1<<" "<<vec[i].size()<<endl;
		printf("%d %d\n",i+1,vec[i].size());
		sort(vec[i].begin(),vec[i].end(),cmp);
		for(int j=0;j<vec[i].size();j++){
			//cout<<vec[i][j].str<<endl;
			printf("%s\n",vec[i][j].str);
		}
	}
	#ifndef ONLINE_JUDGE
	fclose(stdin);
	#endif
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值