[PAT]1039 Course List for Student

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

Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤40,000), the number of students who look for their course lists, and K (≤2,500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni(≤200) are given in a line. Then in the next line, Ni student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.

Output Specification:
For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student's name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.

Sample Input:
11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9
Sample Output:
ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0

思路:由题意得输入给的是课程关于学生的映射(即一个课程可能对应多个学生),而输出是学生关于课程的映射(一个学生对应多个课程),因此想到map容器,可以用学生姓名作为key值,选课编号作为value值,因为一个学生涉及多种选课,所以用vector<int>类型来存储选课信息,存在value值里

代码实现:

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
	int N, K, i, j, ID_course, stu_count, k;
	string name;
	map<string, vector<int> >student;
	cin >> N >> K;
	for (i = 0;i < K;i++)
	{
		cin >> ID_course >> stu_count;
		for (j = 0,k = 0;j < stu_count;j++)
		{
			cin >> name;
			student[name].push_back(ID_course);//push_back不用事先指定vector长度
			name = "\0";//输入完成后初始化(非必要),若名字非定长,必须初始化
		}
	}
	for (i = 0;i < N;i++)
	{
		cin >> name;
		if (!student[name].size())//NON9学生无映射,故其选课为0
		{
			cout << name << " " << student[name].size();//末尾无空格
		}
		else
		{
			cout << name << " " << student[name].size() << " ";
		}
		sort(student[name].begin(), student[name].end());//对选课进行排序,注意类型
		for (j = 0;j < student[name].size();j++)
		{
			cout << student[name][j];
			if (j != student[name].size() - 1)
			{
				cout << " ";
			}
		}
		if (i != N - 1)
		{
			cout << endl;
		}
	}
	return 0;
}

结果:

 

总结:

1.此类问题若暴力解决则容易超时且不易实现,C++map类容器提供映射,因此可以选择,但使用前应组织好数据结构

2.关于NON9学生无映射的判断,我的想法是判断value值的容量是否为0,其实也可以利用find函数,定义一个迭代器指向map类,然后find(name),通过判断是否指向end来判断有没有映射,代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
	int N, K, i, j, ID_course, stu_count, k;
	string name;
	map<string, vector<int> >student;
	map<string, vector<int> >::iterator it = student.begin();
	cin >> N >> K;
	for (i = 0;i < K;i++)
	{
		cin >> ID_course >> stu_count;
		for (j = 0,k = 0;j < stu_count;j++)
		{
			cin >> name;
			student[name].push_back(ID_course);//push_back不用事先指定vector长度
			name = "\0";//输入完成后初始化(非必要),若名字非定长,必须初始化
		}
	}
	for (i = 0;i < N;i++)
	{
		cin >> name;
		it = student.begin();
		it = student.find(name);
		/*if (!student[name].size())//NON9学生无映射,故其选课为0
		{
			cout << name << " " << student[name].size();//末尾无空格
		}*/
		if (it == student.end())
		{
			cout << name << " " << student[name].size();
		}
		else
		{
			cout << name << " " << student[name].size() << " ";
		}
		sort(student[name].begin(), student[name].end());//对选课进行排序,注意类型
		for (j = 0;j < student[name].size();j++)
		{
			cout << student[name][j];
			if (j != student[name].size() - 1)
			{
				cout << " ";
			}
		}
		if (i != N - 1)
		{
			cout << endl;
		}
	}
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值