1039. Course List for Student (25)

需要自己写一个hash函数,直接用map的话最后一个case会超时

#include<map>  
#include<queue>  
#include<stack>  
#include<cstdio>  
#include<string>  
#include<vector>  
#include<cstring>  
#include<iostream>  
#include<algorithm>  
#include<functional>  
using namespace std;  
typedef long long LL;  
const int maxn = 5e5 + 10;  
int n, k, x, y, tot, M[maxn];  
char s[maxn];  
vector<int> t[maxn];  
  
int get(char *s)  
{  
    return (s[0] - 'A') * 6760 + (s[1] - 'A') * 260 + (s[2] - 'A') * 10 + s[3] - '0';  
}  
  
int main()  
{  
    scanf("%d%d", &n, &k);  
    for (int i = 1; i <= k; i++)  
    {  
        scanf("%d%d", &x, &y);  
        while (y--)  
        {  
            scanf("%s", s);  
            int k = get(s);  
            if (!M[k]) M[k] = ++tot;  
            t[M[k]].push_back(x);  
        }  
    }  
    for (int i = 1; i <= tot; i++) sort(t[i].begin(), t[i].end());  
    while (n--)  
    {  
        scanf("%s", s);  
        printf("%s", s);  
        int k = M[get(s)];  
        if (k)  
        {  
            printf(" %d", t[k].size());  
            for (int i = 0; i < t[k].size(); i++)  
            {  
                printf(" %d", t[k][i]);  
            }  
        }  
        else printf(" 0");  
        printf("\n");  
    }  
    return 0;  
} 


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
//1039 最后一个case超时
map<string,vector<int> > mapStu;
int n,k,x,y;
string name;
bool cmp(int a,int b){
    return a<b;
}
int main(){
    scanf("%d %d",&n,&k);
    while(k--){
        scanf("%d %d",&x,&y);
        while(y--){
            cin>>name;
            mapStu[name].push_back(x);
        }
    }
    while(n--){
        cin>>name;
        map<string,vector<int> >::iterator iter;
        iter=mapStu.find(name);
        if(iter!=mapStu.end()){
            vector<int>:: iterator iter_vec;
            printf("%s %d",name.c_str(),mapStu[name].size());
            sort(mapStu[name].begin(),mapStu[name].end(),cmp);
            for(iter_vec=mapStu[name].begin();iter_vec!=mapStu[name].end();iter_vec++)
                printf("% d",*iter_vec);
        }
        else
            printf("%s %d",name.c_str(),0);
        printf("\n");
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值