PAT 1047 Student List for Course(25 分)

1047 Student List for Course(25 分)

Zhejiang University has 40,000 students and provides 2,500 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 (≤40,000), the total number of students, and K (≤2,500), 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.

解答:

(1).刚开始,我用vector<set<int>>来存储,有超时;string ->int 用的是哈希函数;

(2) 后来看了其他人的代码,明白,set的insert还是比较花时间的,而且是嵌套循环,故可用vector<vector<> >,之后再排序;

(3)vector<vector<string> >的存储方式,会有一个用例超时,可能string的排序比int的排序慢很多吧。

AC代码如下,第一种是借鉴其他人的,用name[40005][5]来存储名字;第二种是自己用哈希函数来实现的,比第一种稍微麻烦些,但都可以AC的。

方法1:AC代码:

#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>

using namespace std;

char name[40005][5];

bool cmp(int v1, int v2)
{
	return (strcmp(name[v1], name[v2]) < 0);
}
int main()
{
	int N, K;
	
	scanf("%d %d", &N, &K);
	vector<vector<int> > vec(K+5);
	for(int j = 0; j < N; ++j)
	{
		int n;
		
		scanf("%s %d", name[j], &n);
		for(int i = 0; i < n; ++i)
		{
			int cid;
			scanf("%d", &cid);
			vec[cid].push_back(j);
		}
	}
	for(int i = 1; i <= K; ++i)
	{
		printf("%d %d\n", i, vec[i].size());
		sort(vec[i].begin(), vec[i].end(), cmp);
		for(vector<int>::iterator iter = vec[i].begin(); iter != vec[i].end(); ++iter)
		{	
			printf("%s\n", name[*iter]);
		}
	}
	return 0;
}

方法2:AC代码如下:

#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>

using namespace std;

//char name[40005][5];
int Hash(char name[])
{
	return ((name[0] - 'A')*26*26*26 + (name[1] - 'A')*26*26 + (name[2] - 'A')*26 + (name[3] - '0'));
}
void reHash(int v, char name[])
{
	name[0] = v / (26*26*26) + 'A';
	name[1] = v / (26*26) % 26 + 'A';
	name[2] = v / 26 % 26 + 'A';
	name[3] = v % 26 + '0';
	name[4] = '\0'; 
}
bool cmp(int v1, int v2)
{
	char name1[5], name2[5];
	reHash(v1, name1);
	reHash(v2, name2);
	return (strcmp(name1, name2) < 0);
}
int main()
{
	int N, K;
	
	scanf("%d %d", &N, &K);
	vector<vector<int> > vec(K+5);
	for(int j = 0; j < N; ++j)
	{
		int n;
		char name[5];
		
		scanf("%s %d", name, &n);
		for(int i = 0; i < n; ++i)
		{
			int cid;
			scanf("%d", &cid);
			vec[cid].push_back(Hash(name));
		}
	}
	for(int i = 1; i <= K; ++i)
	{
		printf("%d %d\n", i, vec[i].size());
		sort(vec[i].begin(), vec[i].end(), cmp);
		for(vector<int>::iterator iter = vec[i].begin(); iter != vec[i].end(); ++iter)
		{	
			char name[5];
			reHash(*iter, name);
			printf("%s\n", name);
		}
	}
	return 0;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值