zoukankan      html  css  js  c++  java
  • 【算法笔记】A1039 Course List for Student

    https://pintia.cn/problem-sets/994805342720868352/problems/994805447855292416

    题意:

      有N个学生,K节课。给出选择每门课的学生姓名,,并在之后给出这N个学生姓名,按顺序输出每个学生选了几门课、哪几门课。

    思路:

       用hash表存储每个学生的选课情况,需要定义一个hash函数把姓名转换为数字。查询时先把课程用sort()排序再输出。

      最后一个测试点408ms,把 string name 改成 char name[] 之后106ms,说明大部分情况下string比较耗时。个别情况下char型数组会比较耗时。传送门

      可以用vector数组保存选课,也可以用set数组来保存,vector数组需要对课程排序,而set数组插入时自动排序但是只能通过迭代器访问(只有vector和string能使用下标或*(it + i)访问),不过对本题来说没有影响。

    vector版

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 26 * 26 * 26 * 10;
     4 const int N = 40010;
     5 vector<int> selectCourse[maxn];
     6 int getID(char name[]){
     7     int id = 0;
     8     for(int i = 0; i < 3; i++){
     9         id = id * 26 + (name[i] - 'A');
    10     }
    11     id = id * 10 + name[3] - '0';
    12     return id;
    13 }
    14 int main(){
    15     char name[5];
    16     int n, k;
    17     scanf("%d %d", &n, &k);
    18     for(int i = 0; i < k; i++){
    19         int course, x;
    20         scanf("%d %d", &course, &x);
    21         for(int j = 0; j < x; j++){
    22             scanf("%s", name);
    23             int id = getID(name);
    24             selectCourse[id].push_back(course);
    25         }
    26     }
    27     for(int i = 0; i < n; i++){
    28         scanf("%s", name);
    29         int id = getID(name);
    30         sort(selectCourse[id].begin(), selectCourse[id].end());
    31         printf("%s %d", name, selectCourse[id].size());
    32         for(int j = 0; j < selectCourse[id].size(); j++){
    33             printf(" %d", selectCourse[id][j]);
    34         }
    35         printf("
    ");
    36     }
    37     return 0;
    38 }

     set版

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 26 * 26 * 26 * 10;
    const int N = 40010;
    set<int> selectCourse[maxn];
    int getID(char name[]){
        int id = 0;
        for(int i = 0; i < 3; i++){
            id = id * 26 + (name[i] - 'A');
        }
        id = id * 10 + name[3] - '0';
        return id;
    }
    int main(){
        char name[5];
        int n, k;
        scanf("%d %d", &n, &k);
        for(int i = 0; i < k; i++){
            int course, x;
            scanf("%d %d", &course, &x);
            for(int j = 0; j < x; j++){
                scanf("%s", name);
                int id = getID(name);
                selectCourse[id].insert(course);
            }
        }
        for(int i = 0; i < n; i++){
            scanf("%s", name);
            int id = getID(name);
            printf("%s %d", name, selectCourse[id].size());
            set<int>::iterator it = selectCourse[id].begin();
            for(; it != selectCourse[id].end(); it++){
                printf(" %d", *it);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    ★一名“标题党”自我修炼的10大技巧
    字符编码笔记:ASCII,Unicode和UTF-8
    字符编码笔记:ASCII,Unicode和UTF-8
    字符编码笔记:ASCII,Unicode和UTF-8
    ★漫画:优秀的程序员具备哪些属性?
    ★漫画:优秀的程序员具备哪些属性?
    Linux 标准目录结构
    hadoop多次格式化后,导致datanode启动不了
    Linux天天见
    我的ipad应用备份
  • 原文地址:https://www.cnblogs.com/chunlinn/p/10688067.html
Copyright © 2011-2022 走看看