PAT A 1039 Course List for Student (25分)

一、思路

(一)、用scanf()、printf()进行string类输入输出以节省执行时间:

1、scanf()输入

string str;
str.resize(n);//必须预申请空间
scanf("%s", &str[0]);

2、printf()输出

printf("%s", str.c_str());
(二)、用unordered_map存储节省执行时间;

二、代码

#include <cstdio>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int N, K;
    scanf("%d %d", &N, &K);
    string name;
    name.resize(5);
    unordered_map<string, vector<int>> ans;
    for( int i = 0, c, Nc; i < K; ++i )
    {
        scanf("%d %d", &c, &Nc);
        for( int j = 0; j < Nc; ++j )
        {
            scanf("%s", &name[0]);
            ans[name].push_back(c);
        }
    }
    for( int i = 0; i < N; ++i )
    {
        scanf("%s", &name[0]);
        sort( ans[name].begin(), ans[name].end(), [] ( int a, int b ) { return a < b; } );
        printf("%s %d", name.c_str(), ans[name].size());
        for( int j = 0; j < ans[name].size(); ++j )
            printf(" %d", ans[name][j]);
        printf("\n");
    }
}

发布了152 篇原创文章 · 获赞 144 · 访问量 9215

猜你喜欢

转载自blog.csdn.net/qq_43749739/article/details/103922513
今日推荐