1039. Course List for Student (25)

本文探讨了一种优化方法,用于处理学生选课信息的问题,通过使用倒排索引技术,改进了时间复杂度,实现了对学生选课列表的高效查询。详细介绍了算法的实现过程,包括数据结构的选择、姓名到索引的映射方式,以及如何遍历存储的课程和选课者列表,最终输出符合要求的学生选课信息。
摘要由CSDN通过智能技术生成

题目如下:

Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=40000), the number of students who look for their course lists, and K (<=2500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni (<= 200) are given in a line. Then in the next line, Ni student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student's name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.

Sample Input:
11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9
Sample Output:
ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0

这道题我最初使用的方法是map<string,vector<int> >来存储数据,然后查找、输出,但是最后一个case会超时,参考了sunbaigui的解法后发现是使用string引起的超时,应该使用char*代替。

需要注意的是,如果使用map来查找char*,会出错,因为map使用==来判断键值,指针不能被正确的处理,因此这里采用倒排索引的方法,所谓倒排索引也就是通过每个属性值都能找到索引。我们现在要通过姓名找到选课信息。

首先使用一个vector<vector<char*> >,外层是课序号容器,每门课对应一个vector<char*>,其内存储的是选这门课的人名列表,这样就能把每门课的选课者记录下来,接下来是用一个vector<int>数组,数组的某些索引是通过姓名计算出来的,姓名到索引的转化通过加权的方式,三位大写字母,为26进制,数字为10进制,XXXD权值分别为26*26*10、26*10、10、1,这样便可以得到姓名到索引的一一对应,只需要遍历存储课程和选课人列表的容器,对于每个人名计算索引,把这个课序号压入相应的选课容器,即可记录下来每个人选的哪几门课,同时也满足了按照输入的顺序进行记录。

注意一个细节,要输入char*,可以通过char *name = new char[4];然后scanf("%s",name);这样的操作来实现。

#include <iostream>
#include <vector>
#include <stdio.h>
#include <algorithm>

using namespace std;

#define MAX_INDEX 26*26*26*10
vector<int> nameHashMap[MAX_INDEX];

int main()
{
    vector<vector<char*> > courceList;
    int N,K;
    cin >> N >> K;
    courceList.resize(K + 1);
    for(int i = 0; i < K; i++){
        int cid,cnt;
        scanf("%d%d",&cid,&cnt);
        for(int j = 0; j < cnt; j++){
            char* name = new char[4];
            scanf("%s",name);
            courceList[cid].push_back(name);
        }
    }
    for(int i = 1; i <= K; i++){
        for(int j = 0; j < courceList[i].size(); j++){
            char* name = courceList[i][j];
            int index = (name[0] - 'A')*26*26*10 + (name[1]-'A')*26*10 + (name[2] - 'A')*10 + (name[3] - '0');
            nameHashMap[index].push_back(i);
        }
    }
    for(int i = 0; i < N; i++){
        char name[4];
        scanf("%s",name);
        printf("%s",name);
        int index = (name[0] - 'A')*26*26*10 + (name[1]-'A')*26*10 + (name[2] - 'A')*10 + (name[3] - '0');
        vector<int> cources = nameHashMap[index];
        printf(" %d",cources.size());
        for(int j = 0; j < cources.size(); j++){
            printf(" %d",cources[j]);
        }
        printf("\n");
    }
    for(int i = 0; i < courceList.size(); ++i)
	{
		for(int j = 0; j < courceList[i].size(); ++j)
		{
			delete[] courceList[i][j];
		}
	}
    return 0;
}


转载于:https://www.cnblogs.com/aiwz/p/6154149.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值