【PAT 1047】Student List for Course

来源:互联网 发布:laravel 批量删除数据 编辑:程序博客网 时间:2024/04/29 14:51

1047. Student List for Course (25)

时间限制
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 5ZOE1 2 4 5ANN0 3 5 2 1BOB5 5 3 4 2 1 5JOE4 1 2JAY9 4 1 2 5 4FRA8 3 4 2 5DON2 2 4 5AMY7 1 5KAT3 3 5 4 2LOR6 4 2 4 1 5
Sample Output:
1 4ANN0BOB5JAY9LOR62 7ANN0BOB5FRA8JAY9JOE4KAT3LOR63 1BOB54 7BOB5DON2FRA8JAY9KAT3LOR6ZOE15 9AMY7ANN0BOB5DON2FRA8JAY9KAT3LOR6ZOE1
题意:给出每个学生选修的课程列表,输出每个课程的学生列表

分析:由于最终的输出是以课程编号为序,所以每个课程有一个vector,存储选这门课的学生编号。 在输出时,要求按照学生姓名排序输出,所以要对N个学生姓名排序;若每次输出都重新排序,则效率低且重复运算,可以一次性对学生姓名排序,然后建立一个映射表map将原有序号与排序后的序号对应起来。

代码:

#include <iostream>#include <fstream>#include <vector>#include <cstring>#include <algorithm>using namespace std;#include "stdio.h"//此代码使用前,需删除下面两行+后面的system("PAUSE")ifstream fin("in.txt");#define cin finstruct Name{char name[5];int id;};int* map = NULL;//映射表指针bool cmp(const Name& A,const Name& B)//根据Name结构体中name字符数组大小排序{const char* aa = A.name;const char* bb = B.name;int res = strcmp(aa,bb);if(res>0)return false;else return true; }bool compare(const int& A,const int& B)//根据映射后大小排序{return map[A]<map[B];}int main(){int n,k;scanf("%d %d",&n,&k);//cin>>n>>k;//char (*p)[5] = new char[n][5];//new 一个五字节大小数组的数组指针Name* rec = new Name[n];vector<int>* course = new vector<int>[k+1];//为每个course建立一个vectorint i,j,c,t;for(i=0;i<n;i++){scanf("%s%d",rec[i].name,&c);//cin>>rec[i].name>>c;rec[i].id = i;for(j=0;j<c;j++){//cin>>t;scanf("%d",&t);course[t].push_back(i);}}sort(rec,rec+n,cmp);//把name排序map = new int[n];for(i=0;i<n;i++)//将name的输入顺序和排序后顺序 映射起来{map[rec[i].id]=i;}int size;for(i=1;i<k+1;i++){size = course[i].size();printf("%d %d\n",i,size);//size为0,也要输出【第二个case】if(size==0)continue;//cout<<i<<' '<<size<<endl;sort(course[i].begin(),course[i].end(),compare);//根据映射的值进行排序for(j=0;j<size;j++){ printf("%s\n",rec[map[course[i][j]]].name);//cout<<rec[map[course[i][j]]].name<<endl;}}system("PAUSE");return 0;}

原创粉丝点击