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 ...
随机推荐
- C# 二维数组 转换成 DataTable
C# 数据转换 Overview C# 窗体操作中,有些比较特别的操作.但是为了方便我们不得不使用一些比较特别的手段. C#中二维数组转DataTable 首先,我们看一下我对二维数组的数据处理.这次 ...
- awk命令使用整理
1. awk默认以空格分隔, 可摘出指定位置的内容, 通常用法如下( 文件名称为file.txt ): 文件中行内容为: 12:3 a 4:56 b awk '{print $1}' ...
- codeforces 1260C. Infinite Fence (数学or裴蜀定理)
只需要验证小间隔在大间隔之间有没有连续的k个 设小间隔为a,大间隔为b,那么a在b之间出现的次数在\(\lfloor \frac{b}{a}\rfloor\)或者\(\lfloor \frac{b}{ ...
- IDEA创建maven项目慢的不行
方法二 下载archetype-catalog.xml文件,在maven的VM Options加上-DarchetypeCatalog=local 默认情况下,创建maven项目是从网络下载catal ...
- RestTemplate调用接口(附有账号密码)
private JSONObject Post(String url, String payload, String username, String password) { RestTemplate ...
- 利用Runtime对Ivar实例变量进行共用的归档和解档方式
一.介绍 在OC中每一个对象持有的变量都是实例变量,实例变量包括成员变量和属性变量,在runtime中用Ivar表示对象的实例变量.其实,runtime源码中可以看到,Ivar也是一个结构体(基本上在 ...
- P3376 网络最大流模板(Dinic + dfs多路增广优化 + 炸点优化 + 当前弧优化)
### P3376 题目链接 ### 这里讲一下三种优化的实现以及正确性. 1.dfs多路增广优化 一般的Dinic算法中是这样的,bfs() 用于标记多条增广路,以至于能一次 bfs() 出多次 d ...
- 码云git常用命令
Git常用操作命令: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库:$ git remote -v 添加 ...
- DataGridView中在新增行时怎样设置每个Cell单元格的字体样式
场景 DataGridView怎样实现添加.删除.上移.下移一行: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10281414 ...
- Python工具库分享
漏洞及渗透练习平台: WebGoat漏洞练习平台: https://github.com/WebGoat/WebGoat webgoat-legacy漏洞练习平台: https://github.co ...