1047. Student List for Course (25)

1.和前面的1039 Course List for Student (25) 相类似,但是这次给出学生选的课程数,求每个课程有多少学生选,分别是谁。

2.刚开始使用vector<set<int>> 格式存储course,后面在输入数据的时候就已经超时。

3.随后改为vector<vector<int>> 存储,在后面进行sort,没有超时。

时间限制
400 ms
内存限制
64000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.

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

 

AC代码如下:

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;

int main(void)
{
	int studentSum, courseSum;
	scanf("%d %d", &studentSum, &courseSum);
	char name[5];
	vector<vector<int>> course(courseSum);
	int *courseIdx = new int[20];
	int nameInt;
	int chooseSum;
	for (int i = 0; i<studentSum; i++)
	{//输入学生即其所选的课程
		scanf("%s", name);
		nameInt = (name[0] - 'A') * 26 * 26 * 10 + (name[1] - 'A') * 26 * 10 + (name[2] - 'A') * 10 + (name[3] - '0');
		scanf("%d", &chooseSum);
		for (int j = 0; j<chooseSum; j++)
		{
			scanf("%d", &courseIdx[j]);
			course[courseIdx[j] - 1].push_back(nameInt);
		}
	}
	for (int i = 0; i<course.size(); i++)
	{//输出各门课程的结果
		printf("%d %d\n", i + 1, course[i].size());
		sort(course[i].begin(), course[i].end());
		for (vector<int>::iterator ite = course[i].begin(); ite != course[i].end(); ite++)
		{//转化学生的名字,进行输出
			name[4] = 0;
			name[3] = (*ite) % 10 + '0';
			name[2] = ((*ite) / 10) % 26 + 'A';
			name[1] = ((*ite) / 10 / 26) % 26 + 'A';
			name[0] = ((*ite) / 10 / 26 / 26) + 'A';
			printf("%s\n", name);
		}
	}

	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注