题目描述

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. Redux 学习笔记

    1:首先安装redux: npm install --save redux 2:引入redux : import { createStore } from 'redux'; //首先创建执行函数,Re ...

  2. nginx安装最简单教程

    [root@bogon ~]# wget -c http://nginx.org/download/nginx-1.7.9.tar.gz #下载安装包 [root@bogon ~]# ls anaco ...

  3. xenserver使用快照创建虚拟机,提示eth0 has different mac错误

    这个报错的意思就是说mac地址错误 我们对比后可以发现,用快照创建的虚拟机和原虚拟机的eth0那个配置文件的 mac地址是一样的,因为mac地址具有唯一性,所以就报这个错,无法配置ip上网 解决方法很 ...

  4. MEMS 硅麦资料收集

    MEMS 硅麦资料收集 PCM 和 I2S 协议的 MEMS Microphone PCM 协议在蓝牙方面比较多,一般都有 PCM 的接口. MEMS Microphone 更加的省电,更方便用于语音 ...

  5. yaml,json,ini这三种格式用来做配置文件优缺点

    适合人类编写:ini > toml > yaml > json > xml > plist可以存储的数据复杂度:xml > yaml > toml ~ jso ...

  6. c/c++ 获取数组长度

    在C/C++中并没有提供直接获取数组长度的函数 c/c++ 获取数组长度其中一种方法是使用sizeof(array) / sizeof(array[0]). 在C语言中习惯上在使用时都把它定义成一个宏 ...

  7. Linux中硬盘物理扇区与文件系统文件对应关系(转)

    1               概述 系统读写文件过程中,如下面内核打印信息,报告读写某个扇区错误.那么我们如何能够通过sector找到读写哪个文件错误? kernel: end_request: I ...

  8. cvs报错: socket exception recv failed

    连接都OK的. 也可以telnet到服务器上去. 网上的各种方法都试了,没法解决. 后来一直在乱试,居然解决了. 就是这样设置的,选中第一个复选框.

  9. format()的简单实用 笔记

    # 关于format和format_map的使用# 如果要使用输出的字符串对其不仅仅是可以使用format,还可以使用ljust/rjust/center来处理,输出当然也可以是使用%来进行操作,但是 ...

  10. Linux平台下停止后台进程脚本编写

    1.场景说明 [root@master ~]# jps -m 33050 Jps -m 3299 NameNode 3747 ResourceManager 9028 ConsoleConsumer  ...