题目描述

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.

输入

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.

输出

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.
 
题意

假设全校有最多40000名学生和最多2500门课程。现给出每门课的选课学生名单,要求输出每个前来查询的学生的选课清单。

输入格式:

输入的第一行是两个正整数:N(≤40000),为前来查询课表的学生总数;K(≤2500),为总课程数。此后顺序给出课程1到K的选课学生名单。格式为:对每一门课,首先在一行中输出课程编号(简单起见,课程从1到K编号)和选课学生总数(之间用空格分隔),之后在第二行给出学生名单,相邻两个学生名字用1个空格分隔。学生姓名由3个大写英文字母+1位数字组成。选课信息之后,在一行内给出了N个前来查询课表的学生的名字,相邻两个学生名字用1个空格分隔。

输出格式:

对每位前来查询课表的学生,首先输出其名字,随后在同一行中输出一个正整数C,代表该生所选的课程门数,随后按递增顺序输出C个课程的编号。相邻数据用1个空格分隔,注意行末不能输出多余空格。

样例输入


BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1

ANN0 BOB5 JAY9 LOR6

ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6

BOB5

AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9

样例输出

ZOE1
ANN0
BOB5
JOE4
JAY9
FRA8
DON2
AMY7
KAT3
LOR6
NON9

用到了映射,vector,sort

小技巧就是在名字字符串相对固定的情况下,我们可以把一个整数值映射。也方便做后面的排序(其int的大小排序刚好对应于名字大小的排序)

第一次用课程序号作为数组下标,超时了

 #include <stdio.h>
#include <vector>
using namespace std; int hashFunc(char S[],int len)
{
int id=;
for(int i=;i<len-;i++)
{
id=id*+(S[i]-'A');
}
id=id*+S[len-]-'';
return id;
} int main()
{
int n,k;
int t,i,g;
vector<int> vi[];
char str[];
scanf("%d %d",&n,&k);
for(i=;i<k;i++)
{
int a,b;
scanf("%d %d",&a,&b);
while(b--)
{
scanf("%s",str);
t=hashFunc(str,);
vi[a].push_back(t);
}
}
while(n--)
{
scanf("%s",str);
printf("%s",str);
int num=;
t=hashFunc(str,);
vector<int> vt;
for(i=;i<=k;i++)
{
for(g=;g!=vi[i].size();g++)
{
if(t==vi[i][g])
{
num++;
vt.push_back(i);
break;
}
}
}
if(num)
{
printf(" %d",num);
for(i=;i!=vt.size();i++)
{
printf(" %d",vt[i]);
}
}
else printf("");
printf("\n");
}
return ;
}

后来用人名的映射作为数组下标,用空间来代替时间

 #include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std; const int maxx = * * * + ;
vector<int> vi[maxx]; int hashFunc(char S[],int len)
{
int id=;
for(int i=;i<len-;i++)
{
id=id*+(S[i]-'A');
}
id=id*+S[len-]-'';
return id;
} int main()
{
int n,k;
int t,i;
char str[];
scanf("%d %d",&n,&k);
for(i=;i<k;i++)
{
int a,b;
scanf("%d %d",&a,&b);
while(b--)
{
scanf("%s",str);
t=hashFunc(str,);
vi[t].push_back(a);
}
}
while(n--)
{
scanf("%s",str);
printf("%s",str);
t=hashFunc(str,);
sort(vi[t].begin(), vi[t].end());
printf(" %d",vi[t].size());
for(i=;i!=vi[t].size();i++)
{
printf(" %d",vi[t][i]);
}
printf("\n");
}
return ;
}

对比上一种

 #include <stdio.h>
#include <set>
#include <map>
#include <algorithm> using namespace std; int hashfun(char str[],int n)
{
int i;
int ans=;
for(i=;i<n-;i++)
{
ans=ans*+str[i]-'A';
}
ans=ans*+str[n-];
return ans;
} int main()
{
int n,k;
char str[];
map<int,set<int> >mp;
set<int>::iterator it;
scanf("%d %d",&n,&k);
for(int i=;i<k;i++)
{
int a,b;
scanf("%d %d",&a,&b);
int t;
while(b--)
{
scanf("%s",str);
t=hashfun(str,);
mp[t].insert(a);
}
}
while(n--)
{
scanf("%s",str);
printf("%s",str);
int t;
t=hashfun(str,);
printf(" %d",mp[t].size());
for(it=mp[t].begin();it!=mp[t].end();it++)
{
printf(" %d",*it);
}
printf("\n");
}
return ;
}

Course List for Student的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  7. 第三题 有如下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 ...

  8. 编写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 ...

  9. Student管理系统

    使用三层架构实现Student管理系统,分为Studrnt.Model层,Student.DAL层,Student.BLL层和Student.UI层 步骤分析: 1.在Student.Model添加S ...

  10. Linq查询非泛型集合要指定Student类型(比如List)

    #region Linq to 集合查询非泛型集合要指定Student类型            //ArrayList list = new ArrayList();            //li ...

随机推荐

  1. ONVIF让NVR与网络监控摄像机更"亲密"

    NVR的发展目前看主要分为二大类,一类是接入级的嵌入式NVR,其主要针对主流的IP摄像机研发的一种NVR. 另一类是针对社区和平安城市级的大型NVR,其主要采用的是以高端服务器软.硬件结构为基础,以传 ...

  2. openstack--8--控制节点部署Dashboard

    Horizon介绍 Dashboard服务,这里具体的产品就是Horizon1.它提供一个Web界面操作Openstack的系统2.使用Django框架基于Openstack API开发3.支持将Se ...

  3. 【转】C#获取当前日期时间(转)

    我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...

  4. opengl 几何着色器

    绘制4条线段 #define GLEW_STATIC #include <GL/glew.h> #include <GLFW/glfw3.h> #include "S ...

  5. laravel 打印完整sql语句

    laravel5 用DB自带的getQueryLog方法直接打印: DB::connection()->enableQueryLog(); // 开启QueryLog \App\User::fi ...

  6. Tornado 模板(StaticFileHandler/static_path/template_path等) 笔记

    使用StaticFileHandler进行首页默认访问页面,最好将StaticFileHandler放在最后面,这样不会覆盖要匹配自定义的路径 import tornado.web import to ...

  7. TCP加速锐速SS(ServerSpeeder)破解版一键安装

    速(serverspeeder),是一款TCP加速程序,能够增强VPS/服务器连接的稳定性,且有效的提高服务器的带宽利用率,进而提高访问速度.老左经常看到论坛.群里有用户提到锐速这款软件可以提高VPS ...

  8. 前端实现在线预览pdf、word、xls、ppt等文件

    最近在做一个公司的资源管理系统,一些知识小记一下. 1.前端实现pdf文件在线预览功能 方式一.pdf文件理论上可以在浏览器直接打开预览但是需要打开新页面.在仅仅是预览pdf文件且UI要求不高的情况下 ...

  9. 分布式超级账本Hyperledger里zookeeper的作用

    Zookeeper是一种在分布式系统中被广泛用来作为:分布式状态管理.分布式协调管理.分布式配置管理.和分布式锁服务的集群.kafka增加和减少服务器都会在Zookeeper节点上触发相应的事件kaf ...

  10. DataGridView之DataError

    解决思路一: private void dgvChargeList_DataError(object sender, DataGridViewDataErrorEventArgs e) { bool ...