假设全校有最多40000名学生和最多2500门课程。现给出每门课的选课学生名单,要求输出每个前来查询的学生的选课清单。
输入格式:
输入的第一行是两个正整数:N(≤40000),为前来查询课表的学生总数;K(≤2500),为总课程数。此后顺序给出课程1到K的选课学生名单。格式为:对每一门课,首先在一行中输出课程编号(简单起见,课程从1到K编号)和选课学生总数(之间用空格分隔),之后在第二行给出学生名单,相邻两个学生名字用1个空格分隔。学生姓名由3个大写英文字母+1位数字组成。选课信息之后,在一行内给出了N个前来查询课表的学生的名字,相邻两个学生名字用1个空格分隔。
输出格式:
对每位前来查询课表的学生,首先输出其名字,随后在同一行中输出一个正整数C,代表该生所选的课程门数,随后按递增顺序输出C个课程的编号。相邻数据用1个空格分隔,注意行末不能输出多余空格。
输入样例:
10 5
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6
输出样例:
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
代码示例如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct classList{
int cid;
struct classList* next;
}CL;
typedef struct classTable{
CL* head;
CL* tail;
int cnt;
}CT;
const int N=26*26*26*10+1;
CT* st[26*26*26*10+1];
//hash
int hs(char s[]){
return (int)((s[3]-'0')+(s[2]-'A')*10+(s[1]-'A')*26*10+(s[0]-'A')*26*26*10);
}
int main()
{
// printf("%d %d",hs("ZZZ9"),26*26*26*10+1);
for(int i=0;i<N;i++){
CT* t=(CT*)malloc(sizeof(CT));
t->head=(CL*)malloc(sizeof(CL));
t->head->next=NULL;
t->cnt=0;
t->tail=t->head;
st[i]=t;
}
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=k;i++){
int bn,stn;
scanf("%d%d",&bn,&stn);
for(int j=0;j<stn;j++){
char sid[5];
scanf("%s",sid);
int id=hs(sid);
CL* t=(CL*)malloc(sizeof(CL));
t->cid=bn;
t->next=NULL;
st[id]->tail->next=t;
st[id]->tail=st[id]->tail->next;
(st[id]->cnt)++;
}
}
for(int i=0;i<n;i++){
char sid[5];
scanf("%s",sid);
printf("%s",sid);
int id=hs(sid);
printf(" %d",st[id]->cnt);
CL* p=st[id]->head->next;
//桶排序
int* bk=(int*)malloc(sizeof(int)*(k+1));
for(int i=1;i<=k;i++){
bk[i]=0;
}
while(p){
bk[p->cid]=1;
p=p->next;
}
for(int i=1;i<=k;i++){
if(bk[i]==1){
printf(" %d",i);
}
}
free(bk); //不加这句话内存超限。
printf("\n");
}
return 0;
}