首页 > 其他 > 详细

1047 Student List for Course

时间:2020-02-13 17:41:37      阅读:50      评论:0      收藏:0      [点我收藏+]

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 ;
}

技术分享图片

 

1047 Student List for Course

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!