题目描述

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. CSVN(SVN)命令入门及使用过程中遇到的错误问题汇总

    首先进入web管理界面新建一个版本库 新建一个文件text svn add text #如果提示错误,在后面增加–force svn ci -m 'add text' #如果提示错误,将csvn下的目 ...

  2. day 50 Java Script 学习

    前端基础之JavaScript   JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中) ...

  3. Singer 学习十二 指南

    版本0.3.0 tap是一个应用程序,需要一个配置文件和可选的状态文件作为输入,并产生有序的流记录, 状态和模式信息作为输出. 一个记录是任何类型的JSON编码的数据.tap 状态消息用于保留一个调用 ...

  4. cos migration工具webhook推送

    上一篇讲了腾讯云同步工具的使用,这篇主要是补充如何将同步结果主动消息通知. 因为cos migration 工具是java语言,并在github开源的,所以可以直接修改源码,添加webhook推送代码 ...

  5. react-hot-loader 3.0于1.3的区别

    现在react-hot-loader 3.0版本应该还是beta版本,不过没关系,还是可以正常使用,我在项目中用的是react-hot-loader 3.0.0-beta.7 版本,并没用发现任何问题 ...

  6. 简单的user-based协同过滤算法示例代码

    #构造一份打分数据集1 users = {"小明": {"中国合伙人": 5.0, "太平轮": 3.0, "荒野猎人" ...

  7. monkey如何获取app包名

    别人学习网址:http://www.51testing.com/html/58/15092658-2984032.html 使用aapt    aapt是sdk自带的一个工具,在sdk\builds- ...

  8. linux 安装多个版本JDK,指定tomcat的jdk版本

    JDK的下载可以直接到官网下载,这里不再介绍 一.安装JDK 7 vi /etc/profile #set java environmentexport JAVA_HOME=/usr/java/jdk ...

  9. Hanlp在java中文分词中的使用介绍

    项目结构 该项目中,.jar和data文件夹和.properties需要从官网/github下载,data文件夹下载 项目配置 修改hanlp.properties: 1 #/Test/src/han ...

  10. RedHat6.5安装单机flume1.6

    版本号: RedHat6.5   JDK1.8   apache-flume-1.6.0 1.apache-flume-1.6.0-bin.tar.gz 下载 官网下载地址:http://archiv ...