Timus 1446. Sorting Hat 分类问题
来源:互联网 发布:图片ps软件 编辑:程序博客网 时间:2024/05/16 14:31
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 students N (1 ≤ N ≤ 1000). In the next 2N lines 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
7Ivan IvanovGryffindorMac Go NagoloHufflepuffZlobeus ZleiSlytherinUm BridgeSlytherinTatiana Henrihovna GrotterRavenclawGarry PotnyjGryffindorHerr Mionag-RangerGryffindor
Slytherin:Zlobeus ZleiUm BridgeHufflepuff:Mac Go NagoloGryffindor:Ivan IvanovGarry PotnyjHerr Mionag-RangerRavenclaw: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;}
2 0
- Timus 1446. Sorting Hat 分类问题
- Ural 1446. Sorting Hat
- timus 1280 Topological Sorting
- 垃圾分类 garbage sorting
- Timus - 1110. Power 除尽问题
- 约瑟夫问题 线段树Timus OJ 1521
- Timus 1791 汽车过桥的问题
- Timus 1777. Anindilyakwa 奇怪的数数问题
- red hat一些问题
- [BOI2007]名次排序问题(sorting)
- timus 1052. Rabbit Hunt URAL 解题报告 多点共线问题
- Timus 1404. Easy to Hack! 有一个密码问题
- Sorting
- Sorting:
- Sorting
- Sorting
- Sorting
- Sorting
- 通过闪回恢复误删除的数据
- 货币基金从万份收益计算七日年化收益率
- css
- js 中的window.location.search.match("XX")
- 黑马程序员_7K面试题:银行调度系统
- Timus 1446. Sorting Hat 分类问题
- WEB应用的跨域调用
- hdu 2084数塔问题
- sizeof知多少
- 弹球小例子
- pox组件介绍
- Android 获取屏幕尺寸与密度
- 强连通分量(tarjan算法)
- 一分钟掌握Service生命周期