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的更多相关文章

  1. 算法笔记——C/C++语言基础篇(已完结)

    开始系统学习算法,希望自己能够坚持下去,期间会把常用到的算法写进此博客,便于以后复习,同时希望能够给初学者提供一定的帮助,手敲难免存在错误,欢迎评论指正,共同学习.博客也可能会引用别人写的代码,如有引 ...

  2. 学习Java 以及对几大基本排序算法(对算法笔记书的研究)的一些学习总结(Java对算法的实现持续更新中)

    Java排序一,冒泡排序! 刚刚开始学习Java,但是比较有兴趣研究算法.最近看了一本算法笔记,刚开始只是打算随便看看,但是发现这本书非常不错,尤其是对排序算法,以及哈希函数的一些解释,让我非常的感兴 ...

  3. 算法笔记--数位dp

    算法笔记 这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392 数位dp的精髓是不同情况下sta变量的设置. 模板: ]; ...

  4. 算法笔记--lca倍增算法

    算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...

  5. 算法笔记--STL中的各种遍历及查找(待增)

    算法笔记 map: map<string,int> m; map<string,int>::iterator it;//auto it it = m.begin(); whil ...

  6. 算法笔记--priority_queue

    算法笔记 priority_queue<int>que;//默认大顶堆 或者写作:priority_queue<int,vector<int>,less<int&g ...

  7. 算法笔记--sg函数详解及其模板

    算法笔记 参考资料:https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html sg函数大神详解:http://blog.csdn.net/l ...

  8. 算法笔记_067:蓝桥杯练习 算法训练 安慰奶牛(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是 ...

  9. 算法笔记(c++)--回文

    算法笔记(c++)--回文 #include<iostream> #include<algorithm> #include<vector> using namesp ...

  10. 算法笔记(c++)--完全背包问题

    算法笔记(c++)--完全背包和多重背包问题 完全背包 完全背包不同于01背包-完全背包里面的东西数量无限 假设现在有5种物品重量为5,4,3,2,1  价值为1,2,3,4,5  背包容量为10 # ...

随机推荐

  1. [Selenium] 在Grid模式下打印出当前Case是在哪台Node上运行

    AAAbstractFlow() public void getComputerNameOfNode(WebDriver driver){ String CIHub = Environment.get ...

  2. MySql 8小时解决方案:proxool连接池

    最近做的项目用的mysql数据库,前天挂在服务器上,昨天早晨上班一来,同事就说API数据接口访问不了了,我马上mstsc登陆服务器看,报错了.马上重启tomcat,结果还能正常运行,当时没管,今天过来 ...

  3. .net 分布式架构之分布式锁实现(转)

    分布式锁是控制分布式系统之间同步访问共享资源的一种方式.在分布式系统中,常常需要协调他们的动作.如果不同的系统或是同一个系统的不同主机之间共享了一个或一组资源,那么访问这些资源的时候,往往需要互斥来防 ...

  4. fakeapp, faceswap, deepfacelab等deepfakes换脸程序的简单对比

    https://deepfakes.com.cn/index.php/95.html https://www.cnblogs.com/zackstang/p/9011753.html

  5. 23 DesignPatterns学习笔记:C++语言实现 --- 2.7 Proxy

    23 DesignPatterns学习笔记:C++语言实现 --- 2.7 Proxy 2016-07-18 (www.cnblogs.com/icmzn) 模式理解

  6. VS中的Debug 和 Release 编译方式的本质区别

    VS中的Debug 和 Release 编译方式的本质区别 Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序.Release 称为发布版本,它往往是进行了各种优化,使 ...

  7. How To Make A DFF Read Only Through Form Personalisations? (文档 ID 1289789.1)

    In this Document   Goal   Solution   References APPLIES TO: Oracle Application Object Library - Vers ...

  8. IDEA配置spring

    大半天都在看spring,以前总是看不下去,这次慢慢来,慢慢看. 看那些基础的,倒是还不错.好多都是关于helloworld的,写完helloworld,觉得不怎么形象.于是写了动物,作为接口. (1 ...

  9. MSP430 G2553 Launchpad实现电容测量

    一.基本原理 对于Source-Free RC电路,其电容放电的特性可以描述为: 其中V0是电容的初始电压,t是放电时间,R是串接的电阻阻值,C是电容值,v(t)是t时刻电容上的电压.因此,若已知V0 ...

  10. [Erlang30]Erlang shell是如何工作的?

    一些关于Erlang启动进程的分析:希望你会喜欢. 原英文地址:http://ferd.ca/repl-a-bit-more-and-less-than-that.html 研究Erlang shel ...