At the start of each school year, a very important event happens at Hogwarts. Each of the first-year wizards and witches is assigned to one of the four Hogwarts houses. The bravest children are put
to Gryffindor, the cleverest are put to Ravenclaw, the most hard-working go to Hufflepuff, and Slytherin becomes home to the most ambitious. The assignment is carried out in the Great Hall of Hogwarts castle in the following way: when the name of a first-year
student is called, he or she comes out to the center of the Hall and puts on the famous Sorting Hat. The Hat estimates the situation in the head of the young wizard or witch and cries out the name of the house to which the student is assigned. A special elf
writes down the Hat's decisions. After the sorting, the elf must quickly compile lists of students of each house. Members of the Society for the Promotion of Elfish Welfare beg you to help the elf in this hard work.
Input
The first line contains the number of first-year studentsN(1 ≤N≤ 1000). In the next 2Nlines there are their names followed by houses in which the Sorting Hat placed them.
A student's name may contain lowercase and uppercase English letters, spaces and hyphens. Each name contains not more than 200 symbols.
Output
Output lists of students of each house in the following format. In the first line there is the name of the house, then a colon, and in the next lines there is the list of students, one in a line. The
lists must be given in the following order: Slytherin, Hufflepuff, Gryffindor, Ravenclaw. There must be empty lines between the lists. In each list, names must be given in the order in which they were called out during the sorting. It is guaranteed that each
list will contain at least one student.
Sample
input
output
7
Ivan Ivanov
Gryffindor
Mac Go Nagolo
Hufflepuff
Zlobeus Zlei
Slytherin
Um Bridge
Slytherin
Tatiana Henrihovna Grotter
Ravenclaw
Garry Potnyj
Gryffindor
Herr Mionag-Ranger
Gryffindor
|
Slytherin:
Zlobeus Zlei
Um Bridge
Hufflepuff:
Mac Go Nagolo
Gryffindor:
Ivan Ivanov
Garry Potnyj
Herr Mionag-Ranger
Ravenclaw:
Tatiana Henrihovna Grotter
|
这是个利用map来分类的问题。
注意一下getline的运用,会把之前遗漏下来的换行符都继续读取的,所以记得要去掉之前有输入而又不使用getline读入的换行符。
利用一个数据结构:unordered_map<string, vector<string>>就能解决问题了
#include <string>
#include <vector>
#include <iostream>
#include <unordered_map>
using namespace std;
namespace{
static const int HOUSES = 4;
string houses[HOUSES] = {"Slytherin","Hufflepuff","Gryffindor","Ravenclaw"};
}
void SortingHat1446()
{
int n = 0;
cin>>n;
string name, houseName;
cin.ignore();//注意:去掉这个dumb换行符
unordered_map<string, vector<string> > umSVS;
for (int i = 0; i < n; i++)
{
getline(cin, name);
getline(cin, houseName);
umSVS[houseName].push_back(name);
}
for (int i = 0; i < HOUSES; i++)
{
vector<string> tmp = umSVS[houses[i]];
cout<<houses[i]<<":\n";
for (int j = 0; j < (int)tmp.size(); j++)
{
cout<<tmp[j]<<endl;
}
cout<<endl;
}
}
int main()
{
SortingHat1446();
return 0;
}
分享到:
相关推荐
An Adaptive Generic Sorting Algorithm that Uses Variable Partitioning。pdf.pdfAn Adaptive Generic Sorting Algorithm that Uses Variable Partitioning。pdf.pdfAn Adaptive Generic Sorting Algorithm that ...
some useful little program for new learner Graph Alg.:Red-Black tree,Dijkstra s algorithm,Depth first search and breadth... Sorting:Various array sorting algorithms.. Searching:Array searching algorithms
The sorting.exe
8. Sorting. 9. Tables and Information Retrieval. 10. Binary Trees. 11. Multiway Trees. 12. Graphs. 13. Case Study: The Polish Notation. Appendix A: Mathematical Methods. Appendix B: Random ...
SortingTest.exe
Getting Started with Data Warehousing, 数据仓库理论。 Draft 版本
The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...
SP-7098: Fixed a bug where re-sorting the list of code analysis issues caused all expanders to collapse (when grouping by issue type). SP-7131 : Fix crashes that occur when using a new licence.
auto_sorting.py
Sorting This is an Android App to show diferent sorting algorithms working. I'm a Test Analist and as developer I'm at Junior level. If yout would help this project, fell free to join. For now i just ...
该文章介绍了在光谱图像中采用独立分量分析的算法对西红柿进行分类
Java实现常用sorting算法,包括insertion, merge, bubble, heap, quick, couting, radix, bucket and maxHeap/Priority queue sorting。并对算法复杂度使用场景做了分析。 主要参考资料wikipedia, CLRS算法教材
Algorithm-sorting-visualization.zip,生成gif的命令行工具,可以显示排序算法,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
go程序设计语言 Contents Preface................................................................................................................................. xix PART 1—WHY LEARN GO—GETTING ...
Part 1: Overview and Configuration Understanding Project Management...........................................................21 Why Use Project Portfolio Management?.....................................
哈利·波特·排序帽子 Sorting Hat是Hogwarts的一种有感觉的帽子,它神奇地确定了每个新学生最属于四个学校房屋中的哪个。... Sorting Hat根据用户给出的一系列答案得出的分析结果将您分类到不同的房子中。
The Way to Go,: A Thorough Introduction to the Go Programming Language 英文书籍,已Cross the wall,从Google获得书中源代码,分享一下。喜欢请购买正版。 目录如下: Contents Preface......................
howto-sorting.pdf
Backbone.Collection.sorting 一个用于 Backbone 集合的简单排序插件解决方案,具有可配置的排序方向和类型,包括数字、字母、日期和布尔值。 该插件使用支持各种排序顺序类型和方向的新...collection.sorting.by 选
javascript、sorting、algorithm