1. 程式人生 > >資料結構實驗之排序七:選課名單(裡面包含了新思想!!!!好神奇)

資料結構實驗之排序七:選課名單(裡面包含了新思想!!!!好神奇)

Attention:

如果struct node 裡的陣列開的太大,會導致MLE!!!

 

資料結構實驗之排序七:選課名單

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

隨著學校規模的擴大,學生人數急劇增加,選課名單的輸出也成為一個繁重的任務,我校目前有在校生3萬多名,兩千多門課程,請根據給定的學生選課清單輸出每門課的選課學生名單。

Input

輸入第一行給出兩個正整數N( N ≤ 35000)和M(M ≤ 2000),其中N是全校學生總數,M是課程總數,隨後給出N行,每行包括學生姓名拼音+學號後兩位(字串總長度小於10)、數字S代表該學生選課的總數,隨後是S個課程編號,約定課程編號從1到M,資料之間以空格分隔。

 

Output

按課程編號遞增的順序輸出課程編號、選課總人數以及選課學生名單,對選修同一門課程的學生按姓名的字典序輸出學生名單。資料之間以空格分隔,行末不得有多餘空格。

Sample Input

5 3
Jack01 2 2 3
Jone01 2 1 3
Anni02 1 1
Harry01 2 1 3
TBH27 1 1

Sample Output

1 4
Anni02
Harry01
Jone01
TBH27
2 1
Jack01
3 3
Harry01
Jack01
Jone01

 

#include<bits/stdc++.h>
using namespace std;

struct node
{
    int num;
    char name[101];
    struct node *next;
}*nam[35001];

int b[35001];
int main(void)
{
    int n, m, i, x, shu;
    char w[35];
    memset(b, 0, sizeof(b));
    scanf("%d %d", &n, &m);
    for(i = 1; i <= m; i++)
    {
        nam[i] = new struct node;
        nam[i]-> next = NULL;
    }
    for(i = 0; i < n; i++)
    {
        scanf("%s %d", w, &x);
        while(x--)
        {
            scanf("%d", &shu);
            b[shu]++;
            struct node *p = nam[shu];
            struct node *q;
            q = (struct node *)malloc(sizeof(struct node));
            strcpy(q-> name, w);
            q-> next = NULL;
            while(p-> next != NULL)
            {
                if(strcmp(q-> name, p-> next-> name) < 0)
                {
                    break;
                }
                p = p-> next;
            }
            q-> next = p-> next;
            p-> next = q;

        }
    }

    for(i = 1; i <= m; i++)
    {
        printf("%d %d\n", i, b[i]);
        struct node *p = nam[i]-> next;
        while(p)
        {
            printf("%s\n", p-> name);
            p = p-> next;
        }
    }
    return 0;
}