题目描述

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. mac-ppt-auto-open-recovery-files

    mac 每次打开PPT都会出现一个自动保存的文件,不知道这个文件是保存在哪里,该怎么删除 打开finder(访达),按 shift+command+G,输入~/Library/Containers/c ...

  2. microcks 微服务mocks 工具&&运行时

    microcks 是一个方便的微服务mock 工具,我们可以用来mock request.response,同时获取api 的服务契约,microcks 支持的部署模式也比较多,docker-comp ...

  3. 扩大UIButton的选区

          设计为了美观.button有时设计的太小,这么一来是好看了.可是button不好点中了.曾经有非常多解决方法,如用hitTest等方法. 都比較复杂.后来我发现一个简单的方法. 原理就是U ...

  4. NPOI之Excel——合并单元格、设置样式、输入公式、设置筛选等

    首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...

  5. mysql之 sysbench0.4.12数据库性能测试

    1. 常用选项 在Shell中运行以下命令便可得到sysbench的常用选项信息: sysbench --help 上述命令的输出信息如下图所示: 1.1 使用语法 使用sysbench时,可以通过以 ...

  6. 关于Bagging

    Bagging分为两种:Bagging和Pasting,前者是概率中的放回随机采样,后者是不放回随机采样:默认是放回采样随机:设置bootstrap=False即设置为不放回采样:默认bootstra ...

  7. Ionic 使用karma进行单元测试

    1. 创建Ionic工程 ionic start projectname cd projectname 2.安装karma插件 npm install karma karma-jasmine karm ...

  8. 中文自然语言处理工具hanlp隐马角色标注详解

    本文旨在介绍如何利用HanLP训练分词模型,包括语料格式.语料预处理.训练接口.输出格式等. 目前HanLP内置的训练接口是针对一阶HMM-NGram设计的,另外附带了通用的语料加载工具,可以通过少量 ...

  9. PHP性能监测的工具介绍 - XHProf -参考自https://jingyan.baidu.com/article/7082dc1c173359e40a89bd95.html

    XHProf 这个软件本是Facebook内部的一个应用工具,2009年3月份开源,为PHP的性能监测提供了很好的工具.官方的介绍中提到: 方法/步骤     XHProf 这个软件本是Faceboo ...

  10. Oracle迁移到MySQL性能下降的注意点(转)

    背景:最近有较多的客户系统由原来由Oracle改造到MySQL后出现了性能问题CPU 100%,或是后台的CRM系统复杂SQL在业务高峰的时候出现堆积导致业务故障.在我的记忆里面淘宝最初从Oracle ...