题目描述

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. firefox一搜索就提示是否进入***网站和取消占地方的标题栏

    来看一下这个蛋疼的提示 每次都要手动关闭.后来在网上看到一个解决方法 解决方法: 地址栏输入about:config回车进入设置, 去掉警告那个勾 点击确定,进入配置页 搜索 取消最上面方人的fire ...

  2. Eclipse无法编译,提示错误“找不到或者无法加载主类”解决方法

    jar包问题: 1.项目的Java Build Path中的Libraries中有个jar包的Source attachment指为了一个不可用的jar包, 解决办法是:将这个不可用的jar包remo ...

  3. Morris

    Morris /*Morris遍历树: *一:如果一个结点有左子树会到达此节点两次(第二次到达结点的时候左子树的所有结点都遍历完成),第一次遍历左子树最后 * 节点为nullptr,第二次遍历指向他自 ...

  4. java自学总结

    经过了一段时间的java学习,感觉自己在编程方面还只是一个初学者,感觉学会了c,在学c++的时候就是以c为基础,java应该也是以c或者c++为基础,但是并非如此,java和c++虽然有一些相似之处, ...

  5. WebSocket概念

    WebSocket 是什么? WebSocket 是一种网络通信协议.RFC6455 定义了它的通信标准. WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议 ...

  6. webpack 入口:entry

    定义一个入口点就生成一个chunk.如果你只是用字符串的方式定义了一个入口点,其就被命名为main.如果你用对象的方式定义多个入口点,其就被命名为入口对象中的键值.下面两个例子是等价的: entry: ...

  7. 函数调用运算符"()"

    14.8函数调用运算符"()"1.函数调用运算符必须是成员函数,一个类可以定义多个不同版本的调用运算符,但是他们相互之间应该在参数数量或返回类型上有所区别.定义了调用运算符的类的对 ...

  8. thinkphp5 怎么获取当前的模块,控制器和方法名

    //当前模块url地址 $request= Request::instance(); $module_name=$request->module(); $controller_name=$req ...

  9. jmeter --响应断言详解

    jmeter --响应断言详解 响应断言 :对服务器的响应进行断言校验 (1)应用范围: main sample and sub sample, main sample only , sub-samp ...

  10. 读DataSnap源代码(三)

    function TWebRequestHandler.HandleRequest(Request: TWebRequest; Response: TWebResponse): Boolean; va ...