Problem Description
随着学校规模的扩大,学生人数急剧增加,选课名单的输出也成为一个繁重的任务,我校目前有在校生3万多名,两千多门课程,请根据给定的学生选课清单输出每门课的选课学生名单。
Input
输入第一行给出两个正整数N( N ≤ 35000)和M(M ≤ 2000),其中N是全校学生总数,M是课程总数,随后给出N行,每行包括学生姓名拼音+学号后两位(字符串总长度小于10)、数字S代表该学生选课的总数,随后是S个课程编号,约定课程编号从1到M,数据之间以空格分隔。
Output
按课程编号递增的顺序输出课程编号、选课总人数以及选课学生名单,对选修同一门课程的学生按姓名的字典序输出学生名单。数据之间以空格分隔,行末不得有多余空格。
Example Input
5 3 Jack01 2 2 3 Jone01 2 1 3 Anni02 1 1 Harry01 2 1 3 TBH27 1 1
Example Output
1 4 Anni02 Harry01 Jone01 TBH27 2 1 Jack01 3 3 Harry01 Jack01 Jone01
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
char name[15];
struct node *next, *last;
} node;
int main()
{
int n, m, i, t, x;
char a[15];
int num[2010];
node * cur[2010];
scanf("%d%d", &n, &m);
memset(num, 0, sizeof(num));
for(i = 0; i < 2010; i++)
cur[i] = NULL;
while(n--)
{
scanf("%s", a);
scanf("%d", &t);
while(t--)
{
scanf("%d", &x);
num[x]++;
node *p, *q;
p = (node *)malloc(sizeof(node));
strcpy(p -> name, a);
if(num[x] == 1)
{
p -> next = cur[x];
cur[x] = p;
p -> last = NULL;
}
else
{
q = cur[x];
while(q -> next)
{
if(strcmp(q -> name, a) > 0)
break;
q = q -> next;
}
if(q == cur[x])
{
if(strcmp(q -> name, a) > 0)
{
p -> next = q;
q -> last = p;
cur[x] = p;
p -> last = NULL;
}
else
{
p -> next = NULL;
q -> next = p;
p -> last = q;
}
}
else if(strcmp(q -> name, a) < 0)
{
q -> next = p;
p -> next = NULL;
p -> last = q;
}
else
{
p -> next = q;
p -> last = q -> last;
q -> last -> next = p;
q -> last = p;
}
}
}
}
for(i = 1; i <= m; i++)
{
node *p;
if(num[i] != 0)
{
p = cur[i];
printf("%d %d\n", i, num[i]);
while(p -> next)
{
printf("%s\n", p -> name);
p = p -> next;
}
printf("%s\n", p -> name);
}
}
return 0;
}