A1039 Course List for Student (25 分)
一、技术总结
- 这里由于复杂度的限制,只能够使用vector,然后进行字符串转化:考虑到string、cin、cout会超时,可以使⽤用hash(262626*10+10)将学⽣生姓名变为int型,然后存储在vector里
- 这里出了一个巨大的问题,就是审题不清导致最后格式结果不正确。
- 还有就是空格输出问题,要注意前面是否已经有输出了。
- 还有就是字符串转化成int存储,hash列表的应用,空间换时间
int getID(char name[]){
int id = 0;
for(int i = 0; i < strlen(name); i++){
id = 26*id + (name[i] - 'A');
}
return id;
}
二、参考代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
const int N = 40010;
const int M = 26*26*26*10 + 1;
vector<int> selectCourse[M];
int getID(char name[]){
int id = 0;
for(int i = 0; i < 3; i++){
id = id*26 + (name[i] - 'A');
}
id = id*10 + (name[3] - '0');
return id;
}
int main(){
int n,k,id = 0;
char name[5];
cin >> n >> k;
for(int i = 0; i < k; i++){
int coursenum, number;
scanf("%d%d", &coursenum, &number);
for(int j = 0; j < number; j++){
scanf("%s", name);
id = getID(name);
selectCourse[id].push_back(coursenum);
}
}
/*
for(int i = 0; i < n; i++) {
scanf("%s", name);
id = getID(name);
sort(selectCourse[id].begin(),selectCourse[id].end());
printf("%s %lu", name, selectCourse[id].size());
for(int j = 0; j < selectCourse[id].size(); j++)
printf(" %d", selectCourse[id][j]);
printf("\n"); }
return 0;
*/
char str[n+1][5];
for(int i = 0; i < n; i++){
scanf("%s", str[i]);
}
for(int i = 0; i < n; i++){
int id = getID(str[i]);
sort(selectCourse[id].begin(), selectCourse[id].end());
printf("%s %d", str[i], selectCourse[id].size());
for(int j = 0; j < selectCourse[id].size(); j++){
//if(j != 0) printf(" ");
printf(" %d", selectCourse[id][j]);
}
printf("\n");
}
return 0;
}
A1039 Course List for Student (25 分)的更多相关文章
- PAT 甲级 1039 Course List for Student (25 分)(字符串哈希,优先队列,没想到是哈希)*
1039 Course List for Student (25 分) Zhejiang University has 40000 students and provides 2500 cours ...
- 1039 Course List for Student (25分)
Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists o ...
- PAT 1039 Course List for Student (25分) 使用map<string, vector<int>>
题目 Zhejiang University has 40000 students and provides 2500 courses. Now given the student name list ...
- 【PAT甲级】1039 Course List for Student (25 分)(vector嵌套于map,段错误原因未知)
题意: 输入两个正整数N和K(N<=40000,K<=2500),分别为学生和课程的数量.接下来输入K门课的信息,先输入每门课的ID再输入有多少学生选了这门课,接下来输入学生们的ID.最后 ...
- PAT 甲级 1047 Student List for Course (25 分)(cout超时,string scanf printf注意点,字符串哈希反哈希)
1047 Student List for Course (25 分) Zhejiang University has 40,000 students and provides 2,500 cou ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- 1109 Group Photo (25 分)
1109 Group Photo (25 分) Formation is very important when taking a group photo. Given the rules of fo ...
- 1012 The Best Rank (25 分)
1012 The Best Rank (25 分) To evaluate the performance of our first year CS majored students, we cons ...
- 1036 Boys vs Girls (25 分)
1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of ...
随机推荐
- oracle--共享磁盘挂载
01,查看挂载的磁盘 [root@SHLPDBWX01 ~]# fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders Units ...
- something just like this---About Me
endl:JX弱校oier,04年生,妹子,2019级高一新生,然后居然不知道该说什么了,尴尬 2019年3月开始接触oi,学的很慢(看起来脑子不太好用) 2019年7月创建了这个博客,在收到“恭喜! ...
- Java类加载机制以及双亲委派模型
一.Java类加载机制 1.概述 Class文件由类装载器装载后,在JVM中将形成一份描述Class结构的元信息对象,通过该元信息对象可以获知Class的结构信息:如构造函数,属性和方法等,Java允 ...
- .NET Core 内置的 System.Text.Json 使用注意
System.Text.Json 是 .NET Core 3.0 新引入的高性能 json 解析.序列化.反序列化类库,武功高强,但毕竟初入江湖,炉火还没纯青,使用时需要注意,以下是我们在实现使用中遇 ...
- golang数据结构之单链表
实现单链表的增删查改. 目录如下: singleLink.go package link import ( "fmt" ) //HeroNode 链表节点 type HeroNod ...
- php date获取前一天的时间
结果: 结论: 第二种方式只使用了一个函数,所以更快一些,速度大约是第一种的两倍
- 1+x 证书 Web 前端开发 HTML5 专项练习
官方QQ群 1+x 证书 Web 前端开发 HTML5 专项练习 http://blog.zh66.club/index.php/archives/193/
- git分支合并创建切换
1. 场景描述 介绍下Git最新内容合并到主干.从主干创建最新分支.idea下切换最新分支,能在2分钟内完成git合并.分支创建以及在idea中完成切换,希望能帮到一些朋友. 2. 解决方案 从以下三 ...
- pycharm问题解析(connecting to console)
1. 场景描述 以前一直用的anaconda3,临时下载了demo用的python2,就下载anaconda2安装了下,测试过后,发现pycharm中以前的项目跑不起来了,一直报:connecting ...
- 使用logging模块进行封装,让bug无处遁寻
import logging from scripts.handle_config import conf from scripts.constants import LOGS_DIR class H ...