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# List<T> 转 DataTable
C# List<T>转DataTable 学习自:博客园 Overview 数据!!个人认为程序就是将数据变着花样的显示它.那么这个时候我们的数据处理和获取就时我们的关键一步,如果你数据都 ...
- 基于SincNet的原始波形说话人识别
speaker recognition from raw waveform with SincNet Mirco Ravanelli, Yoshua Bengio 作为一种可行的替代i-vector的 ...
- 【2016NOI十连赛2-2】黑暗
[2016NOI十连赛2-2]黑暗 题目大意:定义一个无向图的权值为连通块个数的\(m\)次方.求\(n\)个点的所有无向图的权值和.多次询问. 数据范围:\(T\leq 1000,n\leq 300 ...
- 【nginx启动报错】重启服务器之后nginx启动错
错误信息: # ./nginx nginx: [emerg] open() "/var/run/nginx/nginx.pid" failed (2: No such file ...
- springboot实践1
环境安装 安装jdk 推荐安装jkd1.8+,我使用的是mac,假设已经安装好homebrew,则jdk的安装指令是: brew install java 在 ~/zshrc ,添加两行 export ...
- CAT 默认密码修改
修改操作 1.按照如下路径,打开SessionManager类,cat-home目录下:com.dianping.cat.system.page.login.service.SessionManage ...
- [IDA] 将变量索引进行计算
按 k 键 [ebp+var+arg_0] - > [ebp+value]
- StreamWriter StreamReader
private void WriteLoginJsonData(object jsonData) { using (FileStream writerFileStream = new FileStre ...
- 科技风商务项目管理PPT模板
模板来源:http://ppt.dede58.com/peixunyanjiang/26267.html
- QNetworkRequest加Authorization头,适应Rest风格的API
Rest是无状态的.Rest的请求之间不应该有依赖,在调用一个请求前,不需要一定要去提前调用另外一个请求.Rest里面不应该有 session,特别是Rest请求不应该保存信息在sesssion里,以 ...