【算法笔记】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] 获取当前Project所在的路径
String projectPath = System.getProperty ("user.dir").toString()
- spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />
spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...
- pthread_rwlock_rdlock和“No such file or directory”
pthread_rwlock_rdlock和"No such file or directory" 调用pthread_rwlock_rdlock时,如果失败报错"pth ...
- Java Annotation Processors
Table Of Contents 1. Introduction 2. When to Use Annotation Processors 3. Annotation Processing Unde ...
- EventBus事件总线框架(发布者/订阅者模式,观察者模式)
一. android应用内消息传递的方式: 1. handler方式-----------------不同线程间传递消息. 2. Interface接口回调方式-------任意两个对象. 3. In ...
- VS2017安装时自动退出
电脑重装系统后 win10,安装VS2017,一直不成功:保持一定时间后就自动退出,而没有跳到具体安装选择项界面.重复了好几次,并且电脑也重启还是这样. 最后在系统盘下面找到文件夹:C:\Progra ...
- php数组转成php编程代码
将php数组转成可以在php上面运行的编程代码,支持一维及多维数组 <?php //一维数组 $test1 = array(1,2,3); //二维数组 $test2[0] = array( ' ...
- [Erlang36]kerl轻松管理安装各种OTP版本
kerl只有一个目标:让我们在不同的OTP版本间随意切换.他是一个纯Bash项目.简单实用的工作利器! Readme里面用法已说明得非常清楚了.建议按流程来一次. 1.下载 安装(一个bash脚本,根 ...
- Buffer Pool--内存相关术语
虚拟地址空间(virtual address space): 供应用程序能够申请访问的最大地址空间,32位系统上为4GB,64位系统上是8TB,虚拟地址空间映射的数据不一定存放在物理内存中,还可能存放 ...
- /usr/bin/curl: Argument list too long的解决方法
使用curl发送http请求时,会出现-bash: /usr/bin/curl: Argument list too long的错误,此时,可用采用httpie代替curl发送请求: pip inst ...