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 ...
随机推荐
- React中引用CSS样式的方法
相对于html中引用css的三种方法,react中也有三种方法,一一相对: 1. 行内样式:直接在组件内部定义 <div style={{width:'20px',height:'30px'}} ...
- Codechef November Challenge 2019 Division 1
Preface 这场CC好难的说,后面的都不会做QAQ 还因为不会三进制位运算卷积被曲明姐姐欺负了,我真是太菜了QAQ PS:最后还是狗上了六星的说,期待两(三)场之内可以上七星 Physical E ...
- 用OC基于数组实现循环队列
一.简言 使用数组实现循环队列时,始终会有一个空余的位置预留着,作为一个判决队列已满的条件(当然也可以采用其他方式).在前面已经用C++介绍了基本的算法,可以去回顾一下https://www.cnbl ...
- SpringMVC方法的返回值类型和自动装配
1. void类型作为返回值类型 /** * 如果方法写成了void就跟原来servlet含义是差不多 的 * json */ @RequestMapping("/firstRequest& ...
- jQuery 源码分析(十四) 数据操作模块 类样式操作 详解
jQuery的属性操作模块总共有4个部分,本篇说一下第3个部分:类样式操作部分,用于修改DOM元素的class特性的,对于类样式操作来说,jQuery并没有定义静态方法,而只定义了实例方法,如下: a ...
- Web前端—— JQuery迷你版实现以及使用
JQuery迷你版实现以及使用 tiny_jquery.js var $ = function (selector) { var ele = document.querySelector(select ...
- python基础(26):类的成员(字段、方法、属性)
1. 字段 字段:包括普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同. 普通字段属于对象 静态字段属于类 字段的定义和使用: class Province: # ...
- 易优CMS:关于assign你知道多少
[基础用法] 名称:assign 功能:模板文件中定义变量,可在其他标签里使用该变量 语法: {eyou:assign name='typeid' value='5' /} 文件: 无 参数: nam ...
- 在Vue-cli3.x中引入element-ui的新方式
今天登上element官网,发现element对vue-cli3.x的项目做了特殊定制: 这意味着即使是按需引入,也无需像过去那样手动安装babel-plugin-component,配置babel. ...
- 使用element-ui的el-menu导航选中后刷新页面保持当前选中
<el-menu :default-active=‘$route.path‘ :router=‘true‘ :unique-opened=‘true‘ :default-openeds=&quo ...