1039. Course List for Student (25)

来源:互联网 发布:淘宝开网店用18 编辑:程序博客网 时间:2024/05/17 23:34

开始用map<string,set<int>>存储,发现最后一个测试点超时,后来看了牛客网的评论,发现可以构建一个hash表来代替map,A-Z A-Z A-Z 0-9,然后神奇的过了,果然map的速度还是不够快

#include<iostream>#include<set>#include<vector>#include<algorithm>#include<string>#pragma warning(disable:4996)using namespace std;int hashcode(char *s) {    int ret = 0;    for (int i = 0;i<3;i++) {        ret = ret * 27 + s[i] - 'A';    }    ret = ret * 10 + s[3] - '0';    return ret;}set<int> stu[200000];int main(){    int N, K;    scanf("%d %d", &N, &K);    for (int t = 0;t < K;t++)    {        int index, i;        scanf("%d %d", &index, &i);        while (i--)        {            char a[10];            scanf("%s", a);            int temp = hashcode(a);            stu[temp].insert(index);        }    }    for (int t = 0;t < N;t++)    {        char a[10];        scanf("%s", a);        int temp=hashcode(a);        printf("%s %d", a, stu[temp].size());        for (auto x : stu[temp])            printf(" %d",x);        printf("\n");    }}
0 0
原创粉丝点击