1039. Course List for Student (25)

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

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 (<=40000), the number of students who look for their course lists, and K (<=2500), 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 思路 1.题目比较简单,关键是优化问题,如果直接用map<string,vector<int>>最后一个用例会超时,因为本身对map和string的操作相比char + 整数来说更耗时间。
2.所以得自己写一个简单的散列函数将名字散列为vector的整数索引来代替map,另外使用char数组存储名字而不是string,输入输出最好也换成scanf和printf而不是cin、cout。 超时代码
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std; int main()
{
int N,K;
while(cin >> N >> K)
{
map<string,vector<int>> students;
students.clear();
for(int i = 0;i < K;i++)
{
int course,num;
cin >> course >> num;
for(int j = 0;j < num;j++)
{
string name;
cin >> name;
students[name].push_back(course);
}
}
for(int i = 0;i < N;i++)
{
string name;
cin >> name;
cout << name << " " << students[name].size();
sort(students[name].begin(),students[name].end());
for(int j = 0;j < students[name].size();j++)
{
cout << " " << students[name][j];
}
cout << endl;
}
}
}
AC代码
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int maxnum = 26 * 26 * 26 * 10 + 1; int HashName(char* name)
{
return (name[0] - 'A') * 26 * 26 * 10 +
(name[1] - 'A') * 26 * 10 +
(name[2] - 'A') * 10 +
(name[3] - '0');
} int main()
{
int N,K;
while(cin >> N >> K)
{
vector<vector<int>> students(maxnum);
char name[5];
for(int i = 0;i < K;i++)
{
int course,num;
cin >> course >> num;
for(int j = 0;j < num;j++)
{
scanf("%s",name);
students[HashName(name)].push_back(course);
}
}
for(int i = 0;i < N;i++)
{
scanf("%s",name);
int index = HashName(name);
printf("%s %lu", name, students[index].size());
sort(students[index].begin(),students[index].end());
for(int j = 0;j < students[index].size();j++)
{
cout << " " << students[index][j];
}
cout << endl;
}
}
}

  

PAT1039: Course List for Student的更多相关文章

  1. PAT1047: Student List for Course

    1047. Student List for Course (25) 时间限制 400 ms 内存限制 64000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  2. java.io.NotSerializableException: test.io.file.Student

    java.io.NotSerializableException: test.io.file.Student    at java.io.ObjectOutputStream.writeObject0 ...

  3. 使用java反射机制编写Student类并保存

    定义Student类 package org; public class Student { private String _name = null; ; ; public Student() { } ...

  4. 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  5. The constructor User.Student(String, String, String) is not visible

    项目:蒙文词语检索 日期:2016-05-01 提示:The constructor User.Student(String, String, String) is not visible 出处:Db ...

  6. 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; ...

  7. student表中创建触发器,实现student表和student _course表的级联删除

    create trigger Delete_sc on student for delete as delete student_course where student_course.s_no in ...

  8. 第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; 其中,classNum 表示学生的班号,例如“class05”。 有如下List List list = new ArrayList();

    list.add(new Student("Tom", 18, 100, "class05")); list.add(new Student("Jer ...

  9. 编写Java应用程序。首先,定义描述学生的类——Student,包括学号(int)、 姓名(String)、年龄(int)等属性;二个方法:Student(int stuNo,String name,int age) 用于对对象的初始化,outPut()用于输出学生信息。其次,再定义一个主类—— TestClass,在主类的main方法中创建多个Student类的对象,使用这些对象来测 试Stud

    package zuoye; public class student { int age; String name; int stuNO; void outPut() { System.out.pr ...

随机推荐

  1. 01_MUI之Boilerplate中:HTML5示例,动态组件,自定义字体示例,自定义字体示例,图标字体示例

     1安装HBuilder5.0.0,安装后的界面截图如下: 2 按照https://www.muicss.com/docs/v1/css-js/boilerplate-html中的说明,创建上图的 ...

  2. linux 编译安装PHP模块

    本文移到:http://www.phpgay.com/Article/detail/classid/6/id/54.html  linux 编译安装PHP模块 1.首先你要有你服务器上安装的PHP的版 ...

  3. javase--day_01

    一.关键字: /*                            关键字:  被java语言复与特定含义的单词.      特点: 组成关键字单词的字母全部小写.   注意: A:goto和c ...

  4. 集群增量会话管理器——DeltaManager

    DeltaManager会话管理器是tomcat默认的集群会话管理器,它主要用于集群中各个节点之间会话状态的同步维护,由于相关内容涉及到集群,可能会需要一些集群通信相关知识,如果有疑问可结合集群相关章 ...

  5. Android开发技巧——使用Dialog实现仿QQ的ActionSheet菜单

    最近看到有人用Dialog来实现QQ的仿ActionSheet的自定义菜单,对于自己没实现过的一些控件,看着也想实现一下.于是动手了一下,发现也不难,和大家分享一下. 本文原创,转载请注明出处:htt ...

  6. objc直接通过指针访问对象实例变量

    我们现在来做一件被认为是very bad的事情,如题所示:无论实例变量是私有的.保护的都可以通过地址访问到,并且还可以修改之.这可以称之为所谓的"超级键值编码". 首先上代码: # ...

  7. Windows7安装Pygame软件

    1. 工具包下载地址: Python下载地址: https://www.python.org/downloads/ pip下载地址:https://pypi.org/project/pip/#file ...

  8. C#逻辑面试题汇总【不断更新中】

    (1)产生本月的月历,参考样式: 1 2 3 4 5 6 SU MO TU WE TH FR SA          01 02 03 04 05 06 07 08 09 10 11 12 13 14 ...

  9. ORACLE中主键约束跟唯一索引的区别

    分类: DB 2011-12-03 21:34 611人阅读 评论(0) 收藏 举报 oracleconstraintsimmutableusertabledomain 1.  分别用两种方法创建主键 ...

  10. Servlet 学习总结

    Servlet资料整理[很全很强大] 分类: J2EE2009-10-23 00:51 671人阅读 评论(0) 收藏 举报 servletsessionstring服务器initialization ...