zoukankan      html  css  js  c++  java
  • PAT1039: Course List for Student

    1039. Course List for Student (25)

    时间限制
    200 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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 (<=40000), the number of students who look for their course lists, and K (<=2500), 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 Ni (<= 200) are given in a line. Then in the next line, Ni 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

    思路

    1.题目比较简单,关键是优化问题,如果直接用map<string,vector<int>>最后一个用例会超时,因为本身对map和string的操作相比char + 整数来说更耗时间。
    2.所以得自己写一个简单的散列函数将名字散列为vector的整数索引来代替map,另外使用char数组存储名字而不是string,输入输出最好也换成scanf和printf而不是cin、cout。

    超时代码
    #include<iostream>
    #include<vector>
    #include<map>
    #include<algorithm>
    using namespace std;
    
    int main()
    {
        int N,K;
        while(cin >> N >> K)
        {
            map<string,vector<int>> students;
            students.clear();
            for(int i = 0;i < K;i++)
            {
                int course,num;
                cin >> course >> num;
                for(int j = 0;j < num;j++)
                {
                    string name;
                    cin >> name;
                    students[name].push_back(course);
                }
            }
            for(int i = 0;i < N;i++)
            {
                string name;
                cin >> name;
                cout << name << " " << students[name].size();
                sort(students[name].begin(),students[name].end());
                for(int j = 0;j < students[name].size();j++)
                {
                    cout << " " << students[name][j];
                }
                cout << endl;
            }
        }
    }
    AC代码
    #include<iostream>
    #include<vector>
    #include<map>
    #include<algorithm>
    using namespace std;
    const int maxnum = 26 * 26 * 26 * 10 + 1;
    
    int HashName(char* name)
    {
        return (name[0] - 'A') * 26 * 26 * 10 +
               (name[1] - 'A') * 26 * 10 +
               (name[2] - 'A') * 10 +
               (name[3] - '0');
    }
    
    int main()
    {
        int N,K;
        while(cin >> N >> K)
        {
            vector<vector<int>> students(maxnum);
            char name[5];
            for(int i = 0;i < K;i++)
            {
                int course,num;
                cin >> course >> num;
                for(int j = 0;j < num;j++)
                {
                    scanf("%s",name);
                    students[HashName(name)].push_back(course);
                }
            }
            for(int i = 0;i < N;i++)
            {
                scanf("%s",name);
                int index = HashName(name);
                printf("%s %lu", name, students[index].size());
                sort(students[index].begin(),students[index].end());
                for(int j = 0;j < students[index].size();j++)
                {
                    cout << " " << students[index][j];
                }
                cout << endl;
            }
        }
    }
    

      

  • 相关阅读:
    js的深拷贝特别注意this的深拷贝
    快速的熟悉一个angular的项目从run看起
    关于angular路由中的#
    AngularJS的Provider, Value, Constant, Service, Factory, Decorator的区别与详解
    css页面缩放
    jquery自定义window事件
    js自定义事件
    git分支
    webpack知识小结--require.context方法
    Vue 创建组件的两种方法
  • 原文地址:https://www.cnblogs.com/0kk470/p/7871708.html
Copyright © 2011-2022 走看看