1047. Student List for Course (25)

Source: Internet
Author: User

Topics such as the following:

Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course list of each student and you is supposed to output the student name lists of all the courses .

Input Specification:

each input file contains one test case. The first line contains 2 numbers:n (<=40000), the total number of students, and K (<=2500), the Tot Al Number of courses. Then N lines follow, each contains a student ' s name (3 capital 中文版 letters plus a one-digit number), a positive number C (<=20) which is the number of courses that this student have registered, and then followed by C course numbers. For the sake of simplicity, the courses is numbered from 1 to K.

Output Specification:

For each test case, print the student name lists of the courses in increasing order of the course numbers. For each course, first print on one line the course number and the number of registered students, separated by a space. Then output the students ' names in alphabetical order. Each name is occupies a line.

Sample Input:
5zoe1 2 4 5ann0 3 5 2 1bob5 5 3 4 2 1 5joe4 1 2jay9 4 1 2 5 4fra8 3 4 2 5don2 2 4 5amy7 1 5KAT3 3 5 4 2LOR6 4 2 4 1 5
Sample Output:
1 4ann0bob5jay9lor62 7ann0bob5fra8jay9joe4kat3lor63 1bob54 7bob5don2fra8jay9kat3lor6zoe15 9amy7ann0bob5don2fra8jay9kat3lor6zoe1


This is a relatively simple statistical, output problem, because the output does not involve the query, but the sequential output. Therefore, you do not have to use map to reverse the index. Just need to give each course a vector<char*> press all the people who have chosen the course into the corresponding container, then sort by dictionary order, and finally output.

It is important to note that. For character processing, use char* instead of string as much as possible, although string is very handy, but for string comparison and copy operations can be time consuming. Easy timeout.

Char* is not a good string, but in C + + features I can use char* name = new Char[4], to create a space that just fits the name, and then press it into the container.

For char*, I started with strcmp, but it is S1<S2 return -1,S1==S2 return 0,S1>S2 return 1, while the sort function defaults to < and returns 1 for satisfaction, so the two are not very matched. I finally took a manual comparison of each bit of the method.

The code is as follows:

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <map> #include <vector > #include <string> #include <string.h> #include <algorithm>using namespace Std;int compare (const         Char *name1, const char *name2) {for (int i = 0; i < 4; i++) {if (Name1[i] < Name2[i]) {return 1;        }else if (name1[i] = = Name2[i]) {continue;        }else{return 0;    }}}int Main () {int n,k,c,num;    CIN >> N >> K;    Vector<vector<char*> > Course (k+1);        for (int i = 0; i < N; i++) {char* name = new Char[4];        scanf ("%s%d", name,&c);           for (int j = 0; J < C; J + +) {scanf ("%d", &num);        Course[num].push_back (name);        }} for (int j = 1; J <= K; j + +) {sort (Course[j].begin (), course[j].end (), compare);        printf ("%d%d\n", j,course[j].size ()); for (int k = 0; k < course[j].size (); k++) {PrinTF ("%s\n", Course[j][k]); }} return 0;}


1047. Student List for Course (25)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.