Question 196.pat Grade A Exercise - 1047 Student List for Course (25 points)


Question 196.pat Grade A Exercise - 1047 Student List for Course (25 points)


1. The topic

insert image description here
insert image description here

2. Problem solving

When inputting, just put the name into the container represented by the corresponding course. The most important thing to note is that you can only use scanf and printf for input and output, otherwise it will definitely time out. The following is the code for using set to install the person's name, it is still timed out, and the correct code will be given later.

//用set仍然超时,insert会多耗时间的
#include <bits/stdc++.h>

using namespace std;

set<string> s[2501];

int main()
{
    
    
    int N,K;
    scanf("%d%d",&N,&K);
    for(int i=0;i<N;i++)
    {
    
    
        char name[5];//为避免超时,所以我输入只能用scanf,所以不得不用char []
        scanf("%s",name);
        int C;
        scanf("%d",&C);
        for(int j=0;j<C;j++)
        {
    
    
            int course;
            scanf("%d",&course);
            s[course].insert(name);//string(char[]),可将char[]通过构造方法变成string。这里直接insert就好,因为set<string>
        }
    }
    for(int i=1;i<=K;i++)
    {
    
    
        printf("%d %d\n",i,s[i].size());//输出课程编号和课程人数
        for(auto it=s[i].begin();it!=s[i].end();it++)
        {
    
    
            printf("%s\n",(*it).c_str());//将string用.c_str()转成char[]用printf输出,不能用cout否则会超时
        }
    }
}

Change the set to vector to install, and then sort just fine. code show as below:

#include <bits/stdc++.h>

using namespace std;

vector<string> v[2501];

int main()
{
    
    
    int N,K;
    cin>>N>>K;
    for(int i=0;i<N;i++)
    {
    
    
        char name[5];
        scanf("%s",name);
        int C;
        scanf("%d",&C);
        for(int j=0;j<C;j++)
        {
    
    
            int c;
            scanf("%d",&c);
            v[c].push_back(name);
        }
    }
    for(int i=1;i<=K;i++)
    {
    
    
        printf("%d %d\n",i,v[i].size());
        sort(v[i].begin(),v[i].end());
        for(int j=0;j<v[i].size();j++)
        {
    
    
            printf("%s\n",v[i][j].c_str());
        }
    }
}

Using set does not time out (use operator overloading skillfully, sort it when inputting, and then directly reverse it later, no need to sort in dictionary), you can do this, the code is as follows:

/*
#include <bits/stdc++.h>

using namespace std;
struct stuInfo{
    char name[5];
    int C;
    int course[20];
    bool operator < (const stuInfo &x) const{
        return strcmp(name,x.name) <= 0;
    }
    bool operator > (const stuInfo &x) const
    {
        return strcmp(name, x.name) >= 0;
    }
    bool operator == (const stuInfo &x) const
    {
        return strcmp(name, x.name) == 0;
    }
};
set<stuInfo> s;
vector<string> courses[2501];
int main()
{
    int N, K;
    scanf("%d%d", &N, &K);
    stuInfo *stu;
    for (int i = 0; i < N; i++)
    {
        stu=new stuInfo();
        scanf("%s", stu->name);
        scanf("%d", &stu->C);
        for (int j = 0; j < stu->C; j++)
        {
            scanf("%d", stu->course+j);
        }
        s.insert(*stu);
    }
    for(auto it=s.begin();it!=s.end();it++)//由于前面insert操作把名字按字典序排好了所以这里直接倒腾
    {
        for(int i=0;i<it->C;i++)//访问那个人结构体,将它选的课程插入他的名字(不用担心课程的人名字字典序了,因为字典序在前的会先被拿来插入)
        {
            courses[it->course[i]].push_back(it->name);
        }
    }
    for (int i = 1; i <= K; i++)
    {
        printf("%d %d\n", i, courses[i].size()); //输出课程编号和课程人数
        for (auto it = courses[i].begin(); it != courses[i].end(); it++)
        {
            printf("%s\n", (*it).c_str()); //将string用.c_str()转成char[]用printf输出,不能用cout否则会超时
        }
    }
}
*/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324347606&siteId=291194637