Course List for Student

2019/03/30 14:31
阅读数 82

题目描述

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 N i (<= 200) are given in a line. Then in the next line, N i 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个空格分隔,注意行末不能输出多余空格。

样例输入

11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9

样例输出

ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0

 

用到了映射,vector,sort

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

 

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

 1 #include <stdio.h>
 2 #include <vector>
 3 using namespace std;
 4  
 5 int hashFunc(char S[],int len)
 6 {
 7     int id=0;
 8     for(int i=0;i<len-1;i++)
 9     {
10         id=id*26+(S[i]-'A');
11     }
12     id=id*10+S[len-1]-'0';
13     return id;
14 }
15  
16  
17 int main()
18 {
19     int n,k;
20     int t,i,g;    
21     vector<int> vi[40001];
22     char str[5];
23     scanf("%d %d",&n,&k);
24     for(i=0;i<k;i++)
25     {
26         int a,b;
27         scanf("%d %d",&a,&b);
28         while(b--)
29         {
30             scanf("%s",str);
31             t=hashFunc(str,4);
32             vi[a].push_back(t);
33         }
34     }
35     while(n--)
36     {
37         scanf("%s",str);
38         printf("%s",str);
39         int num=0;
40         t=hashFunc(str,4);
41         vector<int> vt;
42         for(i=1;i<=k;i++)
43         {
44             for(g=0;g!=vi[i].size();g++)
45             {
46                 if(t==vi[i][g])
47                 {
48                     num++;
49                     vt.push_back(i);
50                     break;
51                 }
52             }
53         }
54         if(num)
55         {
56             printf(" %d",num);
57             for(i=0;i!=vt.size();i++)
58             {
59                 printf(" %d",vt[i]);
60             }
61         }
62         else printf(" 0");
63         printf("\n");
64     }
65     return 0;
66 }

 

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

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <vector>
 4 using namespace std;
 5  
 6 const int maxx = 26 * 26 * 26 * 10 + 1;
 7 vector<int> vi[maxx];
 8  
 9 int hashFunc(char S[],int len)
10 {
11     int id=0;
12     for(int i=0;i<len-1;i++)
13     {
14         id=id*26+(S[i]-'A');
15     }
16     id=id*10+S[len-1]-'0';
17     return id;
18 }
19  
20  
21 int main()
22 {
23     int n,k;
24     int t,i;    
25     char str[5];
26     scanf("%d %d",&n,&k);
27     for(i=0;i<k;i++)
28     {
29         int a,b;
30         scanf("%d %d",&a,&b);
31         while(b--)
32         {
33             scanf("%s",str);
34             t=hashFunc(str,4);
35             vi[t].push_back(a);
36         }
37     }
38     while(n--)
39     {
40         scanf("%s",str);
41         printf("%s",str);
42         t=hashFunc(str,4);
43         sort(vi[t].begin(), vi[t].end());
44         printf(" %d",vi[t].size());
45         for(i=0;i!=vi[t].size();i++)
46         {
47             printf(" %d",vi[t][i]);
48         }
49         printf("\n");
50     }
51     return 0;
52 }

 对比上一种

 1 #include <stdio.h>
 2 #include <set>
 3 #include <map>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 int hashfun(char str[],int n)
 9 {
10     int i;
11     int ans=0;
12     for(i=0;i<n-1;i++)
13     {
14         ans=ans*26+str[i]-'A';
15     }
16     ans=ans*10+str[n-1];
17     return ans;
18 }
19 
20 int main()
21 {
22     int n,k;
23     char str[5];
24     map<int,set<int> >mp;
25     set<int>::iterator it;
26     scanf("%d %d",&n,&k);
27     for(int i=0;i<k;i++)
28     {
29         int a,b;
30         scanf("%d %d",&a,&b);
31         int t;
32         while(b--)
33         {
34             scanf("%s",str);
35             t=hashfun(str,4);
36             mp[t].insert(a);
37         }
38     }
39     while(n--)
40     {
41         scanf("%s",str);
42         printf("%s",str);
43         int t;
44         t=hashfun(str,4);
45         printf(" %d",mp[t].size());
46         for(it=mp[t].begin();it!=mp[t].end();it++)
47         {
48             printf(" %d",*it);
49         }
50         printf("\n");
51     }
52     return 0;
53 }

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部