【算法笔记】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 # ...
随机推荐
- 随机分布 + action 计数
For random samples from , use: 注意平方: sigma * np.random.randn(...) + mu 2.5*2.5 = 6.25 Two-by-four a ...
- CentOS 7上源码编译安装和配置LNMP Web+phpMyAdmin服务器环境
CentOS 7上源码编译安装和配置LNMP Web+phpMyAdmin服务器环境 什么是LNMP? LNMP(别名LEMP)是指由Linux, Nginx, MySQL/MariaDB, PHP/ ...
- wcf服务编程(第3版)文摘
第1章 wcf基础 什么是wcf: System.ServiceModel.dll 服务 服务的执行边界: proxy 地址:http/https,tcp,ipc,peer newwork,msmq, ...
- net 程序员面试宝典
第1部分 求职过程 ------------------------------------------------------------------------------------------ ...
- RocketMQ runbroker.sh 分析JVM启动参数
runbroker.sh #====================================================================================== ...
- C# Http请求接口数据的两种方式Get and Post
面向接口编程是一种设计思想,无论用什么语言都少不了面向接口开发思想,在软件开发过程中,常常要调用接口,接下来就是介绍C#调用其它开发商提供的接口进行获取数据,http接口方式获取接口数据. Get请求 ...
- 从Objective-C到Swift,你必须会的(三)init的顺序
Objective-C的构造函数吧,就最后return一个self.里头你要初始化了什么都可以.在Swift的init函数里把super.init放在前面,然后再初始化你代码里的东西就会报错了. 所以 ...
- 获取当前的window 以及设置其rootViewController
AppDelegate *app = [[UIApplication sharedApplication] delegate]; app.window.rootViewCont ...
- Codeforces768B Code For 1 2017-02-21 22:17 95人阅读 评论(0) 收藏
B. Code For 1 time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 23 DesignPatterns学习笔记:C++语言实现 --- 2.2 Adapter
23 DesignPatterns学习笔记:C++语言实现 --- 2.2 Adapter 2016-07-22 (www.cnblogs.com/icmzn) 模式理解