PAT (Advanced Level) 1039. Course List for Student (25)

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) 1039. Course List for Student (25)相关的知识,希望对你有一定的参考价值。

map会超时,二分吧...

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;

int n,m;
int tot;

vector<int>v[50000];
struct X
{
    int id;
    int num;
    char name[210][6];
}s[2600];

struct Name
{
    char name[6];
}op[40010];

bool cmp2(const Name&a,const Name&b)
{
    return strcmp(a.name,b.name)<0;
}

bool cmp(const X&a,const X&b)
{
    return a.id<b.id;
}

char quary[40010][6];

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&s[i].id);
        scanf("%d",&s[i].num);
        for(int j=1;j<=s[i].num;j++) scanf("%s",s[i].name[j]);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%s",op[i].name);
        strcpy(quary[i],op[i].name);
    }
    sort(op+1,op+1+n,cmp2);

    sort(s+1,s+1+m,cmp);

    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=s[i].num;j++)
        {
            int h=0;
            int l=1,r=n;
            while(l<=r)
            {
                int mid=(l+r)/2;
                if(strcmp(op[mid].name,s[i].name[j])<0) l=mid+1;
                else if(strcmp(op[mid].name,s[i].name[j])>0) r=mid-1;
                else
                {
                    h=mid;
                    break;
                }
            }
            if(h==0) continue;
            v[h].push_back(s[i].id);
        }
    }

    for(int i=1;i<=n;i++)
    {
        int q=0;
        int l=1,r=n;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(strcmp(op[mid].name,quary[i])<0) l=mid+1;
            else if(strcmp(op[mid].name,quary[i])>0) r=mid-1;
            else
            {
                q=mid;
                break;
            }
        }
        if(q==0) continue;

        printf("%s ",quary[i]);
        printf("%d",v[q].size());
        for(int j=0;j<v[q].size();j++) printf(" %d",v[q][j]);
        printf("\n");
    }

    return 0;
}

 

以上是关于PAT (Advanced Level) 1039. Course List for Student (25)的主要内容,如果未能解决你的问题,请参考以下文章

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095

PAT Advanced Level 1038