1047 Student List for Course (25 分)
 

Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤), the total number of students, and K (≤), the total number of courses. Then N lines follow, each contains a student's name (3 capital English letters plus a one-digit number), a positive number C (≤) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.

Output Specification:

For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students' names in alphabetical order. Each name occupies a line.

Sample Input:

10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5

Sample Output:

1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1

题意:

建立一个课程->学生的表即可。由于课程编号是从1-K,所以可以直接用vector当表。

题解:

用string型的vector数组记录每个课程中的学生名字,输出前对其排序。要注意用cout的话最后一个点会超时。

string scanf()

s.resize(); //需要预先分配空间
scanf("%s", &s[]);

string printf()

string s;
s="fdasf";
printf("%s\n",s.c_str());

AC代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
using namespace std;
vector<string>v[];
int main(){
int n,k;
scanf("%d %d",&n,&k);
for(int i=;i<=k;i++) v[i].clear();
for(int i=;i<=n;i++)
{
string s;
/*s.resize(10); //需要预先分配空间
scanf("%s", &s[0]);*/
cin>>s;
int m;
scanf("%d",&m);
for(int j=;j<=m;j++){
int x;
scanf("%d",&x);
v[x].push_back(s);
}
}
for(int i=;i<=k;i++){
printf("%d %d\n",i,v[i].size());
sort(v[i].begin(),v[i].end());
for(int j=;j<v[i].size();j++){
printf("%s\n",v[i].at(j).c_str());
}
}
return ;
}

字符串哈希和反哈希:

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
using namespace std;
vector<int>v[];
char s[];
int getID(char name[]){
int id=;
for(int i=;i<;i++){
id=id*+(name[i]-'A');
}
id=id*+name[]-'';
return id;
}
int main(){
int n,k;
scanf("%d %d",&n,&k);
for(int i=;i<=k;i++) v[i].clear();
for(int i=;i<=n;i++)
{
scanf("%s",&s);
int m;
scanf("%d",&m);
for(int j=;j<=m;j++){
int x;
scanf("%d",&x);
v[x].push_back(getID(s));
}
}
for(int i=;i<=k;i++){
printf("%d %d\n",i,v[i].size());
sort(v[i].begin(),v[i].end());
for(int j=;j<v[i].size();j++){
int x=v[i].at(j);
string s="";
char c=x%+'';
s=s+c;
x=x/;
for(int kk=;kk<=;kk++){
c=x%+'A';
s=c+s;
x=x/;
}
printf("%s\n",s.c_str());
}
}
return ;
}

PAT 甲级 1047 Student List for Course (25 分)(cout超时,string scanf printf注意点,字符串哈希反哈希)的更多相关文章

  1. PAT甲级:1036 Boys vs Girls (25分)

    PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...

  2. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  3. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

  4. PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

    1066 Root of AVL Tree (25 分)   An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...

  5. PAT 甲级 1055 The World's Richest (25 分)(简单题,要用printf和scanf,否则超时,string 的输入输出要注意)

    1055 The World's Richest (25 分)   Forbes magazine publishes every year its list of billionaires base ...

  6. 【PAT甲级】1047 Student List for Course (25 分)

    题意: 输入两个正整数N和K(N<=40000,K<=2500),接下来输入N行,每行包括一个学生的名字和所选课程的门数,接着输入每门所选课程的序号.输出每门课程有多少学生选择并按字典序输 ...

  7. PAT 甲级 1047 Student List for Course

    https://pintia.cn/problem-sets/994805342720868352/problems/994805433955368960 Zhejiang University ha ...

  8. 【PAT甲级】1110 Complete Binary Tree (25分)

    题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...

  9. 【PAT甲级】1062 Talent and Virtue (25 分)

    题意: 输入三个正整数N,L,H(N<=1E5,L>=60,H<100,H>L),分别代表人数,及格线和高水平线.接着输入N行数据,每行包括一个人的ID,道德数值和才能数值.一 ...

随机推荐

  1. UML之关系

    学习UML我们首先要掌握他们的关系,UML关系可以分为四类,主要有关联.依赖.泛化和实现. 下面我们就一一来详细说明这几种关系. 关联 表示两个类或类与接口之间强烈的依赖关系,关联用直线表示.当然我们 ...

  2. 0、Python学习路线

    阶段一.Python语言(熟练掌握Python多线程并发编程技术,可以编写爬虫程序和语音识别软件.) 1.1 基础语法 1.1.1 python概述     1.1.2 数据的存储     1.1.3 ...

  3. Python Scrapy 爬虫框架实例

    之前有介绍 scrapy 的相关知识,但是没有介绍相关实例,在这里做个小例,供大家参考学习. 注:后续不强调python 版本,默认即为python3.x. 爬取目标 这里简单找一个图片网站,获取图片 ...

  4. pygame游戏图像绘制及精灵用法

    1精灵文件 plane_sprites.py import pygame class GameSprite(pygame.sprite.Sprite): """飞机大战游 ...

  5. keil mdk 菜单 “project” 崩溃问题解决

    今天发现我的 Keil MDK5.28z win10系统上面,点击 Project 菜单立即崩溃.网上找到了解决方法 简单粗暴的处理方法:重装keil ,但是依然点击 project 崩溃. 通过搜索 ...

  6. 第十三章 使用ADO.NET访问数据库

    一,什么是ADO.NET ADO.NET 是一种以面向对象的设计方法构建的数据访问和操作的类库,它建立在.NET平台上,以便于操作各种各样的数据源.这些数据源可以是数据库, 也可以是文本文件 ,EXC ...

  7. 字符串转换json格式

    前台json转字符串传递后台时 用到: data: JSON.stringify({ "zh": zhanghao, "mm": mima }), 当后台返回前 ...

  8. mybatis执行insert后马上能获取自增主键的语句写法

    <!--keyColumn keyProperty useGeneratedKeys 用于在插入数据后,能直接使用user.getId()获取主键--> <insert id=&qu ...

  9. Codechef July Challenge 2019 Division 1题解

    题面 \(CIRMERGE\) 破环成链搞个裸的区间\(dp\)就行了 //quming #include<bits/stdc++.h> #define R register #defin ...

  10. 【HDU4622】Reincarnation

    [HDU4622]Reincarnation 一眼似乎不可做,但发现\(strlen(x)\)很小,暴力\(O(n^2)\)预处理每个区间\((l,r)\),查询时\(O(1)\)输出就好了 #inc ...