PAT Advanced 1039 Course List for Student (25分) (STL)

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.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤), the number of students who look for their course lists, and K (≤), 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?? (≤) 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.

Output Specification:

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.

Sample Input:

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

Sample Output:

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

考察STL,用一个map和unordered_map,map主要调整中间key的顺序,unordered_map进行计数。

#include <iostream>
#include <map>
#include <unordered_map>
using namespace std;
int main() {
    int N, K, sid, num;
    string name;
    unordered_map<string, map<int,int>> m;
    cin >> N >> K;
    while(K--) {
        cin >> sid >> num;
        while(num--) {
            cin >> name;
            m[name][sid] = sid;
        }
    }
    while(N--) {
        cin >> name;
        cout << name << " " << m[name].size();
        for(auto it = m[name].begin(); it != m[name].end(); it++)
            cout << " " << it -> first;
        cout << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/littlepage/p/12241006.html

时间: 2024-10-11 04:00:40

PAT Advanced 1039 Course List for Student (25分) (STL)的相关文章

PAT Advanced 1055 The World&#39;s Richest (25分)

Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a certain range of ages. That is, given the n

PAT:1039. Course List for Student (25) 部分正确(最后一个大数据错误)

#include<stdio.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; const int MAX=40010; const int H=26*26*26*10+10; int n,k; //n个人,k门课 vector<int> HAR[H]; int getID(char str[]) //获取名字对应的哈希ID { int

【PAT甲级】1039 Course List for Student (25 分)(vector嵌套于map,段错误原因未知)

题意: 输入两个正整数N和K(N<=40000,K<=2500),分别为学生和课程的数量.接下来输入K门课的信息,先输入每门课的ID再输入有多少学生选了这门课,接下来输入学生们的ID.最后N次询问,输入学生的ID输出该学生选了多少们课,输出所选课程的数量,按照递增序输出课程的ID. trick: 第5个数据点出现段错误,把原本以map存学生对应ID再映射vector存储该学生所选课程改成vector嵌套在map内,就没有段错误的问题出现,疑似映射过程中指针漂移??? 代码: #define H

PAT Advanced 1002 A+B for Polynomials (25分)

This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N?1?? a?N?1???? N?2?? a?N?2?

PAT Advanced 1102 Invert a Binary Tree (25分)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off. Now it's your turn to prove that YOU CAN invert a binary tree! Input Specif

PAT 1039. Course List for Student (25)

1039. Course List for Student (25) 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. In

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any

PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

1066 Root of AVL Tree (25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this

【PAT甲级】1052 Linked List Sorting (25分)

1052 Linked List Sorting (25分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, yo