A1047. Student List for Course
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
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<vector>
- #include<string.h>
- using namespace std;
- char name[][];
- vector<int> course[];
- bool cmp(int a, int b){
- return strcmp(name[a], name[b]) < ;
- }
- int main(){
- int N, K, Ni;
- scanf("%d%d", &N, &K);
- for(int i = ; i < N; i++){
- scanf("%s", name[i]);
- scanf("%d", &Ni);
- for(int j = ; j < Ni; j++){
- int cId;
- scanf("%d", &cId);
- course[cId].push_back(i);
- }
- }
- for(int i = ; i <= K; i++){
- int len = course[i].size();
- printf("%d %d\n", i, len);
- sort(course[i].begin(), course[i].end(), cmp);
- for(int j = ; j < len; j++){
- printf("%s\n", name[course[i][j]]);
- }
- }
- cin >> N;
- return ;
- }
总结:
1、在开二维数组占用空间过大的情况下,可以使用vector<int> num[N],由于初始时每一个vector内均无元素,所以数组可以开的很大。
2、本题需要以课程为主体,存储选课的学生,如果对于每一门课都存储选课的学生名字的话,需要使用string存在vector数组中,需要char[] 与string的互转,比较浪费时间。可以直接把名字作为data存储下来,在vector数组中仅仅存储data的下标即可。 在名字字典序排序时可以用如下方法,用字符串下标代替字符串进行排序:
bool cmp(int a, int b){
return strcmp(name[a], name[b]) < 0;
}
3、sort对vector排序时,区间应填入:sort(vec.begin(), vec.end(), cmp);
A1047. Student List for Course的更多相关文章
- PAT甲级——A1047 Student List for Course
Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course ...
- 【算法笔记】A1047 Student List for Course
https://pintia.cn/problem-sets/994805342720868352/problems/994805433955368960 题意 给出每个学生的选课情况,输出每节课选课 ...
- A1047 Student List for Course (25 分)
一.技术总结 首先题目要看清湖,提出的条件很关键,比如for循环的终止条件,特别注意. 还有这个题目主要考虑到vector的使用,还有注意一定要加上using namespace std; 输出格式, ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- java.io.NotSerializableException: test.io.file.Student
java.io.NotSerializableException: test.io.file.Student at java.io.ObjectOutputStream.writeObject0 ...
- 使用java反射机制编写Student类并保存
定义Student类 package org; public class Student { private String _name = null; ; ; public Student() { } ...
- 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- The constructor User.Student(String, String, String) is not visible
项目:蒙文词语检索 日期:2016-05-01 提示:The constructor User.Student(String, String, String) is not visible 出处:Db ...
- Java-集合-第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; 其中,classNum 表示学生的班号,例如“class05”。 有如下List List list = new ArrayList(); l
第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; ...
随机推荐
- Haproxy和Nginx负载均衡测试效果对比记录
为了对比Hproxy和Nginx负载均衡的效果,分别在测试机上(以下实验都是在单机上测试的,即负载机器和后端机器都在一台机器上)做了这两个负载均衡环境,并各自抓包分析.下面说下这两种负载均衡环境下抓包 ...
- 《Linux内核分析》课程第七周学习总结
姓名:何伟钦 学号:20135223 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/U ...
- C学习随笔
1)要经常复习,一些基础的知识点,学过的.讲过的实例,应多看一下,学习并掌握编程的语法.思路.实验中可看出,不少同学对以前知识没有掌握,对讲过的实例没有理解2)要经常实践,纸上得来终觉浅,绝知此事要躬 ...
- JProfiler的使用
1.下载地址:http://www.ej-technologies.com/download/jprofiler/files 2.使用过程 1.点击此图的new Session 2.点击左边appli ...
- git 使用ssh密钥
一.查看仓库支持的传输协议 1.1查看仓库支持的传输协议 使用命令 git remote -v 查看你当前的 remote url root@zengyue:/home/yuanGit# git re ...
- MySQL 单表优化
一.表字段优化 1.整数类型尽量使用 TINYINT.SMALLINT.MEDIUM_INT 而不是INT,非负数要加上UNSIGNED 2.VARCHAR的长度分配要合理,不要过大 3.时间字段不超 ...
- opencv学习笔记(四)
ROI---设定感兴趣的区域(region of interest) 定义: Mat imageROI; //方法一:通过Rect指定矩形区域 imageROI=image(Rect(500,250, ...
- logback基本入门
1. logback的定义 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分为下面下个模块: logback-core:其它 ...
- Golang 入门~~基础知识
变量声明 //通用形式,指定变量名,变量类型,变量值 var name int = 99 fmt.Println(name) //指定变量名,以及变量类型,未指定值的时候默认是类型零值 var age ...
- Eclipse版本列表
https://wiki.eclipse.org/Older_Versions_Of_Eclipse http://blog.csdn.net/jaycee110905/article/details ...