1047 Student List for Course

1039 Course List for Student 依靠unordered_map<string,set<int>> ans 解决问题。

这次依靠unordered_map<int ,vector<string>> ans;如果vector改成set(自带自排序+去重)最后一个测试点会超时导致无法通过。

所以每次输出结果之前,都要对vector重新排序一次。

STL:unordered_map,vector,sort,string。

#include"iostream"
#include"vector"
#include"unordered_map"
#include"algorithm"
using namespace std;

int main() {
    int n,k,cNum,index;
    scanf("%d%d",&n,&k);
    string name;
    unordered_map<int ,vector<string>> ans;
    for(int i = 0 ; i < n; ++i) {
        cin>>name;
        scanf("%d",&cNum);
        for(int j = 0; j < cNum; ++j) {
            scanf("%d",&index);
            ans[index].push_back(name);
        }
    }
    for(int i = 1; i <= k; ++i) {
        printf("%d %d\n",i,ans[i].size());
        sort(ans[i].begin(),ans[i].end());//对当前vector中的string按字典序排序
        for(auto it = ans[i].begin(); it != ans[i].end(); ++it)
            printf("%s\n",it->c_str());//string 转char* 输出更快
    }
    return 0 ;
}

原文地址:https://www.cnblogs.com/keep23456/p/12303902.html

时间: 2024-12-23 16:27:25

1047 Student List for Course的相关文章

PAT 1047. Student List for Course (25)

1047. Student List for Course (25) Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses. Input Specification: Each

1047 Student List for Course (25 分)

1047 Student List for Course (25 分) Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses. Input Specification: E

1047. Student List for Course (25)

题目例如以下: Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses. Input Specification: Each input file contains one te

PAT Advanced 1047 Student List for Course (25分)

Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses. Input Specification: Each input file contains one test cas

PAT:1047. Student List for Course (25) AC

#include<stdio.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; const int MAX=40010; int n,k; //n个人,k门课 char name[MAX][5]; //存n个人的名字 vector<int> course[MAX]; //记录每个课程选的学生 bool cmp(int a,int b) {

【PAT甲级】1047 Student List for Course (25 分)

题意: 输入两个正整数N和K(N<=40000,K<=2500),接下来输入N行,每行包括一个学生的名字和所选课程的门数,接着输入每门所选课程的序号.输出每门课程有多少学生选择并按字典序输出学生的名字. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;string s[40007];vector<string>v[2507];int main(){ ios::sync_

PAT1047: Student List for Course

1047. Student List for Course (25) 时间限制 400 ms 内存限制 64000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course list of each student, you are supposed to output

PAT A1039、A1047——vector常见用法

A1039.Course List for Student 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 Sp

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10