HDU 3293 由简单排序引发出的一点思考
来源:互联网 发布:淘宝产品卖点 编辑:程序博客网 时间:2024/05/19 09:36
C - sort
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
As is known to all, long long ago sailormoon once was an association of fighters. Till now, sailormoon is also an association of girls. Owe to some unknown reasons, girls are necessary to fight for peace.
Their boss, lcy, wants to strengthen their ability, so he give them his precious collections---weapons for many years. Because these collections are really age-old, it is hard to recognize from one to another. So girls intend to sort them before they use. Each weapon has its name, origin and level of harmfulness ( level contains three ranks: wonderful, good, so-so).
In order to make it clear, girls want to sort like this:
firstly,sort according to the origin (sort by lexicographic order), if two or more have the same origin, they will be sorted together;
secondly, sort according ranks, wonderful is the best, good is next, the third is so-so;
thirdly, if two or more have same origin and rank, sort them according to the lexicographic order.
Their boss, lcy, wants to strengthen their ability, so he give them his precious collections---weapons for many years. Because these collections are really age-old, it is hard to recognize from one to another. So girls intend to sort them before they use. Each weapon has its name, origin and level of harmfulness ( level contains three ranks: wonderful, good, so-so).
In order to make it clear, girls want to sort like this:
firstly,sort according to the origin (sort by lexicographic order), if two or more have the same origin, they will be sorted together;
secondly, sort according ranks, wonderful is the best, good is next, the third is so-so;
thirdly, if two or more have same origin and rank, sort them according to the lexicographic order.
Input
Input contains multiply cases. Each case contains several lines. First line is an integer N(0<N<=500), representing the number of weapons. Then N lines follows. Each line represent a kind of weapon, and contains a set of strings representing name, origin and level of harmfulness.
Each string will not exceed 20 characters.
Sure that same origin will not exist the same weapon.
Each string will not exceed 20 characters.
Sure that same origin will not exist the same weapon.
Output
Please output your list after sorting (format according to sample, pay attention to the spaces,ten spaces need ^ ^).
Sample Input
5knife qizhou so-sogun qizhou wonderfulknife zhengzhou goodstick zhengzhou goodrope shengzhou so-so
Sample Output
Case 1qizhou: gun wonderful knife so-soshengzhou: rope so-sozhengzhou: knife good stick good
题固然是一道很水的题,如果你简单的把他看作一道排序题,那好咱结构体一开,sort的比较函数一写,这题就A了。
那如果你把他看作一道数据结构的题目呢?
如何做到每一个root内的Node排序,后将root排序呢?
也简单用STL容器就行
#include <iostream>#include <algorithm>#include <cstdio>#include <map>#include <set>#include <vector>using namespace std;typedef struct Node{ string wea; string wor; int clas; bool operator <(const Node &x)const { if(clas==x.clas) return wea<x.wea; return clas>x.clas; }};int main(){ int n; int t=0; while(cin>>n){ map <string,set <Node> > x; while(n--){ string wea,bel,wor; cin>>wea>>bel>>wor; Node z; z.wea=wea;z.wor=wor; if(wor=="wonderful") z.clas=3; else if(wor=="good") z.clas=2; else if(wor=="so-so") z.clas=1; x[bel].insert(z); } cout<<"Case "<<++t<<endl; for(map<string,set <Node> >::iterator it1=x.begin();it1!=x.end();it1++){ cout<<it1->first<<':'<<endl; for(set<Node>::iterator it2=x[it1->first].begin();it2!=x[it1->first].end();it2++){ cout<<" "<<it2->wea<<' '<<it2->wor<<endl; } } }}
STL容器的作用:
map:相当于value=f(key)的函数对应关系。即图中的连接线(或边)。
set:相当于一个定义域或是值域(集合)。
即map<key,set<value> >就是一个一对多的对应关系。
而map<key,value>是一对一的对应关系。
用multimap时注意,multimap只对key排序而不会管value。
注:不用queue和stack是因为其不易遍历。
0 0
- HDU 3293 由简单排序引发出的一点思考
- 一点由字符串匹配引发的思考
- 由SpringJdbc引发的一点思考
- 由SpringJdbc引发的一点思考
- 由入栈123出栈顺序引发的思考
- 由“竞争”引发的一点思考 -- 读《ZERO TO ONE》
- 由HDU 2955引发的有关01背包的思考
- 由“人类是由猴子变的么? ”引发的一点思考
- 由简单小问题引发的思考和推理
- 由InvocationTargetException引发的思考
- 由BigDecimal引发的思考
- 由IsPostBack引发的思考
- 由InvocationTargetException引发的思考
- 由B2C引发的思考
- 由cursor引发的思考
- 由size_t引发的思考
- 由volatile引发的思考
- 由一本缺页的"书"引发出一系列思考
- poj3687 反向建图拓扑排序
- 禁掉左右滑动屏幕的ViewPager
- Sql的递归查询
- 关于数据结构中冒泡排序和选择排序的总结
- PAT--1071. Speech Patterns
- HDU 3293 由简单排序引发出的一点思考
- windows下MinGW编译cblas基本线性代数库
- 仿qq侧滑删除Item
- 存储指针的数组
- qt#4信号触发的槽函数到底是哪个线程响应
- Android 状态栏、titlebar的高度计算
- 指针和函数
- 不能返回局部对象的引用
- Android UI性能问题探讨