【算法笔记】A1039 Course List for Student
https://pintia.cn/problem-sets/994805342720868352/problems/994805447855292416
题意:
有N个学生,K节课。给出选择每门课的学生姓名,,并在之后给出这N个学生姓名,按顺序输出每个学生选了几门课、哪几门课。
思路:
用hash表存储每个学生的选课情况,需要定义一个hash函数把姓名转换为数字。查询时先把课程用sort()排序再输出。
最后一个测试点408ms,把 string name 改成 char name[] 之后106ms,说明大部分情况下string比较耗时。个别情况下char型数组会比较耗时。传送门
可以用vector数组保存选课,也可以用set数组来保存,vector数组需要对课程排序,而set数组插入时自动排序但是只能通过迭代器访问(只有vector和string能使用下标或*(it + i)访问),不过对本题来说没有影响。
vector版
#include<bits/stdc++.h>
using namespace std;
const int maxn = * * * ;
const int N = ;
vector<int> selectCourse[maxn];
int getID(char name[]){
int id = ;
for(int i = ; i < ; i++){
id = id * + (name[i] - 'A');
}
id = id * + name[] - '';
return id;
}
int main(){
char name[];
int n, k;
scanf("%d %d", &n, &k);
for(int i = ; i < k; i++){
int course, x;
scanf("%d %d", &course, &x);
for(int j = ; j < x; j++){
scanf("%s", name);
int id = getID(name);
selectCourse[id].push_back(course);
}
}
for(int i = ; i < n; i++){
scanf("%s", name);
int id = getID(name);
sort(selectCourse[id].begin(), selectCourse[id].end());
printf("%s %d", name, selectCourse[id].size());
for(int j = ; j < selectCourse[id].size(); j++){
printf(" %d", selectCourse[id][j]);
}
printf("\n");
}
return ;
}
set版
#include<bits/stdc++.h>
using namespace std;
const int maxn = * * * ;
const int N = ;
set<int> selectCourse[maxn];
int getID(char name[]){
int id = ;
for(int i = ; i < ; i++){
id = id * + (name[i] - 'A');
}
id = id * + name[] - '';
return id;
}
int main(){
char name[];
int n, k;
scanf("%d %d", &n, &k);
for(int i = ; i < k; i++){
int course, x;
scanf("%d %d", &course, &x);
for(int j = ; j < x; j++){
scanf("%s", name);
int id = getID(name);
selectCourse[id].insert(course);
}
}
for(int i = ; i < n; i++){
scanf("%s", name);
int id = getID(name);
printf("%s %d", name, selectCourse[id].size());
set<int>::iterator it = selectCourse[id].begin();
for(; it != selectCourse[id].end(); it++){
printf(" %d", *it);
}
printf("\n");
}
return ;
}
【算法笔记】A1039 Course List for Student的更多相关文章
- 算法笔记——C/C++语言基础篇(已完结)
开始系统学习算法,希望自己能够坚持下去,期间会把常用到的算法写进此博客,便于以后复习,同时希望能够给初学者提供一定的帮助,手敲难免存在错误,欢迎评论指正,共同学习.博客也可能会引用别人写的代码,如有引 ...
- 学习Java 以及对几大基本排序算法(对算法笔记书的研究)的一些学习总结(Java对算法的实现持续更新中)
Java排序一,冒泡排序! 刚刚开始学习Java,但是比较有兴趣研究算法.最近看了一本算法笔记,刚开始只是打算随便看看,但是发现这本书非常不错,尤其是对排序算法,以及哈希函数的一些解释,让我非常的感兴 ...
- 算法笔记--数位dp
算法笔记 这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392 数位dp的精髓是不同情况下sta变量的设置. 模板: ]; ...
- 算法笔记--lca倍增算法
算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...
- 算法笔记--STL中的各种遍历及查找(待增)
算法笔记 map: map<string,int> m; map<string,int>::iterator it;//auto it it = m.begin(); whil ...
- 算法笔记--priority_queue
算法笔记 priority_queue<int>que;//默认大顶堆 或者写作:priority_queue<int,vector<int>,less<int&g ...
- 算法笔记--sg函数详解及其模板
算法笔记 参考资料:https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html sg函数大神详解:http://blog.csdn.net/l ...
- 算法笔记_067:蓝桥杯练习 算法训练 安慰奶牛(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是 ...
- 算法笔记(c++)--回文
算法笔记(c++)--回文 #include<iostream> #include<algorithm> #include<vector> using namesp ...
- 算法笔记(c++)--完全背包问题
算法笔记(c++)--完全背包和多重背包问题 完全背包 完全背包不同于01背包-完全背包里面的东西数量无限 假设现在有5种物品重量为5,4,3,2,1 价值为1,2,3,4,5 背包容量为10 # ...
随机推荐
- Java JarFile 解析
Java JarFile 解析 package com.github.binarylei; import java.io.*; import java.net.URL; import java.net ...
- CentOS 7上源码编译安装和配置LNMP Web+phpMyAdmin服务器环境
CentOS 7上源码编译安装和配置LNMP Web+phpMyAdmin服务器环境 什么是LNMP? LNMP(别名LEMP)是指由Linux, Nginx, MySQL/MariaDB, PHP/ ...
- raw格式转换成qcow2格式
qemu-img convert -f raw redhat6.7-kvm-postgresql9.2.3-8disk-pulsar2.2 -O qcow2 redhat6.7-kvm-postgre ...
- GIT checkout 和 reset 区别
git checkout -- file:撤销对工作区修改:这个命令是以最新的存储时间节点(add和commit)为参照,覆盖工作区对应文件file:这个命令改变的是工作区 git reset HEA ...
- Linux下的用户权限
用户权限: drwxr-xr-x root root - : boot #权限位 硬连接数 所属用户 所属组 大小 最后修改时间 文件/目录# r w x 4 2 1 用户权限位分为3段,分别对应US ...
- Python入门基础学习 一
Python入门基础学习 一 Python下载及安装 下载地址:https://www.python.org/,选择最新的版本下载 稍等一会,安装完成. 简单语句 从idle启动Python:IDLE ...
- Lucene 分页搜索实现
Lucene中有两种分页查询方式 1.一次查询出大量数据,然后根据页码定位是哪个文档,其实就是暴力获取了 2.通过调用searchAfter来实现 我们都知道collect是lucene中对搜索到的文 ...
- 开源WebGIS实施方案(三):Shapefile数据导入到PostGIS
PostGIS新版中提供了一个可视化的工具,用于Shapefile数据的导入和导出,极大的方便了使用者的操作. 创建空间数据库 以具有创建用户权限的账号登录pgAdminIII,连接到数据库 创建一个 ...
- backbone.js 学习笔记
Backbone.Model 模型.相当于表定义,定义一个表当中有的列 defaults:设置属性的默认值 initialize():初始化函数 get(key):获取属性值 set(data):设置 ...
- yum反查某个命令或so库在哪个包里面
yum whatprovides "*/XXX.so.1"