1039. Course List for Student (25)「建议收藏」

1039. Course List for Student (25)

大家好,又见面了,我是全栈君。

题目链接:http://www.patest.cn/contests/pat-a-practise/1039

题目:

1039. Course List for Student (25)

时间限制
200 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=40000), the number of students who look for their course lists, and K (<=2500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni (<= 200) are given in a line. Then in the next line, Ni student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student’s name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.

Sample Input:

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

Sample Output:

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

分析:

输入课程的申请名单,输出每一个人的申请课程。

小技巧就是在名字字符串相对固定的情况下。我们能够把一个整数值映射。也方便做后面的排序(其int的大小排序刚好相应于名字大小的排序)

注意:

这里用了string和cin来做,都是执行超时。或者是异常退出,用char和scanf来做就正常了。

AC代码:

#include<stdio.h>
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<string>
using namespace std;
int Name2Num(char* A){//名字转化为数字
 return (A[0] - 'A') * 26 * 26 * 10 + (A[1] - 'A') * 26 * 10 + (A[2] - 'A') * 10 + A[3] - '0';
}
struct Student{
 vector<int> Courses;
}buf[180000];
int main(){
 //freopen("F://Temp/input.txt", "r", stdin);
 int N, K;
 cin >> N >> K;
 int idx = 0;  //init
 for (int i = 0; i < K; i++){
  int cou_n, m;
  cin >> cou_n >> m;
  for (int j = 0; j < m; j++){
   char name[5];
   scanf("%s", name);
   buf[Name2Num(name)].Courses.push_back(cou_n);//把课程放入学生的空间中
  }
 }
 for (int i = 0; i < N; i++){
  char name[5];
  scanf("%s", name);
  int idx = Name2Num(name);
  sort(buf[idx].Courses.begin(), buf[idx].Courses.end());//排序输出
  cout << name << " " << buf[idx].Courses.size();
  for (int j = 0; j < buf[idx].Courses.size();j ++){
   cout << " " << buf[idx].Courses[j];
  }
  cout << endl;
 }
 return 0;
}

截图:

1039. Course List for Student (25)「建议收藏」

——Apie陈小旭

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115442.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • sql mysql创建 视图索引_SQLServer中在视图上使用索引(转载)「建议收藏」

    sql mysql创建 视图索引_SQLServer中在视图上使用索引(转载)「建议收藏」在SQLServer中,视图是一个保存的T-SQL查询。视图定义由SQLServer保存,以便它能够用作一个虚拟表来简化查询,并给基表增加另一层安全。但是,它并不占用数据库的任何空间。实际上,在你查询它之前,视图并不做任何事情。索引视图在SQLServer2000和2005中,你能够给视图增加索引。但是,如果视图只是一个保存在数据库中的查询定义,在运行前没有自己的数据,你如何给那个定义建立…

    2022年7月22日
    17
  • jvm的垃圾回收机制是什么_垃圾回收过程图片

    jvm的垃圾回收机制是什么_垃圾回收过程图片如果大家对java架构相关感兴趣,可以关注下面公众号,会持续更新java基础面试题,netty,springboot,springcloud等系列文章,一系列干货随时送达,超神之路从此展开,BTAJ不再是梦想!垃圾回收的过程分为两步:1.判断对象是否死亡(1)引用计数器法:①每当有一个对象引用是,计数器加一,当计数器为0是对象死亡②缺点:无法解决循环引用的问题,假设A引用B,B引用A,那么这两个对象将不会被回收,造成内存泄漏(2)可达性算法分析①通过一系列可作为GCRoot

    2025年9月5日
    4
  • 作为Java开发,你一定要了解面向对象编程中为什么要使用get和set方法

    一、由一个问题开始在进行面向对象开发中,在编写一个Class的时候,会定义这个Class的属性(字段)为Private,然后去生成对应的get和set方法,通过这样的方式去对属性进行操作,那你有没有思考过为什么要这样做呢?这样做有哪些好处呢?请先自行思考30秒,在看下面的内容。二、回顾相关知识回答这个问题之前,让我们先回顾一些的基础知识:1、访问修饰符,从访问的范围由小及大依次是:pri…

    2022年2月27日
    46
  • 真是好高兴,CSDN论坛也开了Blog!

    真是好高兴,CSDN论坛也开了Blog!    真的好高兴,我好久没来Csdn了,今天在网上搜资料时看到一篇文章也得很好,一看网址竟是Blog.csdn开头的,我才知道别人都已注册好久了。于是我也来激活了自己的Blog。感觉很不错!于是写了这第一篇文章!希望大家多多支持关注Csdn!愿他越来越兴旺!

    2022年10月2日
    3
  • 计算机网络之ip、子网掩码、网络号、主机号等概念解析

    计算机网络之ip、子网掩码、网络号、主机号等概念解析在工作中谈论到计算机网络时,有几个经常出现的术语,比如:ip、子网掩码、网段等等。之前对这些概念的理解都比较模糊,只知其大概意思,随着工作中遇到的网络问题越来越多,有必要详细理解一下计算机网络的基础知识了。这篇文章就先介绍几个计算机网络领域的专业术语。IP地址ip这个词是计算机网络中出现频率最高的了,甚至只要使用过电脑的人都知道这个词。IP地址全程是互联网协议地址(英文:InternetPr…

    2022年6月24日
    38
  • pytest报错_eclipse提交代码到git

    pytest报错_eclipse提交代码到git前言我们每天写完自动化用例后都会提交到git仓库,随着用例的增多,为了保证仓库代码的干净,当有用例新增的时候,我们希望只运行新增的未提交git仓库的用例。pytest-picked插件可以

    2022年7月28日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号