• 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.

    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 N~i~ (<= 200) 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

    这个比较容易理解但是最后一个测试点不能通过
     1 #include<iostream>
     2 #include<map>
     3 #include<set>
     4 using namespace std;
     5 int main(){
     6   int n, k, i, j;
     7   cin>>n>>k;
     8   map<string, set<int>> mmap;
     9   for(i=0; i<k; i++){
    10     int course, num;
    11     string name;
    12     cin>>course>>num;
    13     for(j=0; j<num; j++){
    14       cin>>name;
    15       mmap[name].insert(course);
    16     }
    17   }
    18   for(i=0; i<n; i++){
    19     string name;
    20     cin>>name;
    21     cout<<name<<" "<<mmap[name].size();
    22     for(auto it=mmap[name].begin(); it!=mmap[name].end(); it++) cout<<" "<<*it;
    23     cout<<endl;
    24   }
    25   return 0;
    26 }

    因为N的取值最大可以达到40000, 如果以string保存字符串肯定为导致输入输出超时; 因而用char数组保存字符串, 但是char数组作为map的关键词是一件很麻烦的事情, 也不方便, 这里把字符串hash成一个整数;

    如果有多行输出,尽量在最后一行末尾输出换行符,否则会有一些测试点不能通过

    对于测试样本的值很大的时候,一定要用printf和scanf, 因为最后一句用的cout<<endl;没能通过最后一个测试点的测试;

    参考:https://www.liuchuo.net/archives/2145

    #include<iostream>
    #include<vector>
    #include<set>
    #include<algorithm>
    
    using namespace std;
    int main(){
      int n, k, i, j, idx;
      scanf("%d%d", &n, &k);
      char name[5];
      vector<int > mmap[26*26*26*10+20];
      for(i=0; i<k; i++){
        int course, num;
        scanf("%d%d", &course, &num);
        for(j=0; j<num; j++){
          scanf("%s", name);
          idx=(name[3]-'0') + (name[2]-'A')*10 + (name[1]-'A')*26*10 + (name[0]-'A')*26*26*10;
          mmap[idx].push_back(course);
        }
      }
      for(i=0; i<n; i++){
        scanf("%s", name);
        idx=(name[3]-'0') + (name[2]-'A')*10 + (name[1]-'A')*26*10 + (name[0]-'A')*26*26*10;
        printf("%s %d", name, mmap[idx].size());
          sort(mmap[idx].begin(), mmap[idx].end());
        for(int i=0; i<mmap[idx].size(); i++) printf(" %d", mmap[idx][i]);
        printf("
    ");
      }
      return 0;
    }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    接口测试入门(5)----新框架重构,使用轻量级的HTTP开发库 Unirest
    接口测试入门(4)--接口自动化测试框架 / list和map用法 / 随机选取新闻 (随机数生成) / 接口相关id映射
    接口测试入门(3)--使用httpClient进行登录用例操作/set-cookies验证/ List<NameValuePair>设置post参数/json解析
    接口测试入门(2)--get和post初级请求/使用httpclient做一个获取信息list的请求(需要登录才可以)
    接口测试学习入门(1)--前期知识储备
    j2ee 使用db.properties连接mysql数据库
    com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别 serverTimezone设定
    mysql JDBC URL格式各个参数详解
    mysql新建数据库时的collation选择(转)
    SpringBoot MyBatis 配置多数据源 (静态多个)
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9155287.html
Copyright © 2020-2023  润新知