1047 Student List for Course——PAT甲级

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.

Sample Input:

复制代码
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

Sample Output:

复制代码
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

solution:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
int main()
{
	int n,k;cin>>n>>k;
	vector<vector<string> >v(k+1);
	for(int i=0;i<n;i++)
	{
		string name;cin>>name;
		int t;cin>>t;
		while(t--)
		{
			int cnt;cin>>cnt;
			v[cnt].push_back(name);
		}
	}
	for(int i=1;i<=k;i++)
	{
		if(v[i].size())
		{
			sort(v[i].begin(),v[i].end());
			cout<<i<<' '<<v[i].size()<<endl;
			for(int j=0;j<v[i].size();j++)
			{
				cout<<v[i][j]<<endl;
			}
		}
		else//测试点1:没人选的课程也要输出
		{
			cout<<i<<' '<<0<<endl;
		}
	}
}

一个简单的二维vector数组排序。

测试点1:没人选的课程也要输出

相关推荐
Geo_V20 分钟前
提示词工程
人工智能·python·算法·ai
云边有个稻草人32 分钟前
Rust 借用分割技巧:安全解构复杂数据结构
数据结构·安全·rust
侯小啾32 分钟前
【22】C语言 - 二维数组详解
c语言·数据结构·算法
qq_4798754335 分钟前
Linux time function in C/C++【2】
linux·c语言·c++
TL滕39 分钟前
从0开始学算法——第一天(如何高效学习算法)
数据结构·笔记·学习·算法
傻童:CPU41 分钟前
DFS迷宫问题
算法·深度优先
B站_计算机毕业设计之家43 分钟前
计算机视觉:python车辆行人检测与跟踪系统 YOLO模型 SORT算法 PyQt5界面 目标检测+目标跟踪 深度学习 计算机✅
人工智能·python·深度学习·算法·yolo·目标检测·机器学习
一个不知名程序员www1 小时前
算法学习入门---前缀和(C++)
c++·算法
jackzhuoa1 小时前
Rust API 设计的零成本抽象原则:从语言基石到工程实践
算法·rust
yuuki2332332 小时前
【数据结构】双向链表的实现
c语言·数据结构·后端