1039. Course List for Student (25)

倒排索引题

这道题目好像是为数不多的一道卡时题,你在PAT刷的很happy的时候,还是要注意下string的滥用的。

因为string的拷贝以及比较操作都要比char数组相同操作的效率低。

另外其实vector的频繁改变数组大小其实也比较耗时,不过在PAT上还是都可以用vector的。

#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

vector<int> name2course[26*26*26*10];
int main()
{
	//input
	int n, k;
	scanf("%d%d",&n,&k);
	//if use string instead of char*, then will be TLE, the copy time is also a bottle neck
	//may be if we do not use vector container and just use array, may be string can be used
	//in such a TLE case, every where we use STL container or cin/cout it can be a bottle neck
	vector<vector<char*>> course(k+1);
	for(int i = 0; i < k; ++i)
	{
		int cid, num;
		scanf("%d%d",&cid,&num);
		course[cid].resize(num);
		for(int j = 0; j < num; ++j)
		{
			char* name = new char[4];
			scanf("%s",name);
			course[cid][j] = name;
		}
	}
	//build the map
	
	for(int i = 1; i <= k; ++i)
	{
		for(int j = 0; j < course[i].size(); ++j)
		{
			char* name = course[i][j];
			int index = (name[0]-'A')*26*26*10+(name[1]-'A')*26*10+(name[2]-'A')*10+(name[3]-'0');
			name2course[index].push_back(i);
		}
	}
	//for query
	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');
		printf(" %d", name2course[index].size());
		for(int j = 0; j < name2course[index].size(); ++j)
		{
			printf(" %d", name2course[index][j]);
		}
		printf("\n");
	}
	//release
	for(int i = 0; i < course.size(); ++i)
	{
		for(int j = 0; j < course[i].size(); ++j)
		{
			delete[] course[i][j];
		}
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值