1039 与 1047 Course List and Student 之 STL 之 vector 与哈希初级

题目

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.
在这里插入图片描述

分析

题意是给出我们每个课程的上课名单,然后会有n个同学来询问他们的课程,如此,我们根据名单输出每个来询问课程的同学的课程。
该题运用STL里的vector能快速解决。

解题步骤

  1. 应为题目给出的查询同学和课程数目较大,所以用哈希表的方法能减小时间,所以容易想到把AAA1(三个字母一个数字组成的姓名)转换为262626*10的数字。
  2. 介绍一下vector,是变长数组,意思是不用规定数组大小,自动根据数组的内容调节大小的数组,然后来复习一下vector的使用:
    1)新建一个vector:vector name,也可以vector name[num]这样新建的就是一个vector的数组,意思是数组里每个元素都有一个vector。
    2)插入:name.push_back(x)
    3)大小:name.size()
    4)与sort搭配:sort(name.begin(), name.end(), cmp)
    直接上代码:
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxv = 26*26*26*11;
vector<int> vt[maxv];
bool cmp(int a, int b)
{
	return a < b;
}
int harsh(char A[])
{
	int ans = 0;
	for(int i = 0; i < 3; i++)
	{
		ans = 26 * ans + (A[i] - 'A');
	}
	ans = ans * 10 +(A[3] - '0');
	return ans;
}
int main()
{
	int n, k; scanf("%d%d", &n, &k);
	char a[5]; int cour, num;
	for(int i = 0; i < k; i++)
	{
		scanf("%d%d", &cour, &num);
		for(int j = 0; j < num; j++)
		{
			scanf("%s", a);
			vt[harsh(a)].push_back(cour);
		}
	}
	for(int i = 0; i < n; i++)
	{
		scanf("%s", a);
		printf("%s %d", a, vt[harsh(a)].size());
		sort(vt[harsh(a)].begin(), vt[harsh(a)].end(), cmp);
		for(int j = 0; j < vt[harsh(a)].size(); j++)
		{
			printf(" %d", vt[harsh(a)][j]);
		}
		printf("\n");
	}
	return 0;
}

小结

1.有字符与数字对应关系想harsh哈希大法好
2.vector与sort搭配大法好

相似题

1047 Student List for Course
Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.
在这里插入图片描述

分析

这道题与上面的题对应,只不过给的条件反过来,把条件和结论反过来出,同样用vector可以轻松解决。
不过很多人可能会绕不过弯来,以为解题的时候也是反过来操作即可,其实中心思想是不变的,还是建立一个课程的vector,只不过因为vector里放字符串可能比较麻烦或低效,所以这里存放的是名字的下标。具体看代码:

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
char name[40010][5];
vector<int> course[2510];
bool cmp(int a, int b)
{
	return strcmp(name[a], name[b]) < 0;
}
int main()
{
	int n, k; scanf("%d%d", &n, &k);
	int cnt, temp;
	for(int i = 1; i <= n; i++)
	{ 
		scanf("%s%d", name[i], &cnt);
		for(int j = 0; j < cnt; j++)
		{
			scanf("%d", &temp);
			course[temp].push_back(i);
		}
	}
	for(int i = 1; i <= k; i++)
	{
		printf("%d %d\n", i, course[i].size());
		sort(course[i].begin(), course[i].end(), cmp);
		for(int j = 0; j < course[i].size(); j++)
		{
			printf("%s\n", name[course[i][j]]);
		}
	}
	return 0;
}

只有建立课程的vector才能更好的排序和输出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值