HDU 3293 由简单排序引发出的一点思考
2016-07-08 18:49
281 查看
C - sort
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status
Description
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.
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.
Output
Please output your list after sorting (format according to sample, pay attention to the spaces,ten spaces need ^ ^).
Sample Input
5
knife qizhou so-so
gun qizhou wonderful
knife zhengzhou good
stick zhengzhou good
rope shengzhou so-so
Sample Output
Case 1
qizhou:
gun wonderful
knife so-so
shengzhou:
rope so-so
zhengzhou:
knife good
stick good
题固然是一道很水的题,如果你简单的把他看作一道排序题,那好咱结构体一开,sort的比较函数一写,这题就A了。
那如果你把他看作一道数据结构的题目呢?
如何做到每一个root内的Node排序,后将root排序呢?
也简单用STL容器就行
STL容器的作用:
map:相当于value=f(key)的函数对应关系。即图中的连接线(或边)。
set:相当于一个定义域或是值域(集合)。
即map<key,set<value> >就是一个一对多的对应关系。
而map<key,value>是一对一的对应关系。
用multimap时注意,multimap只对key排序而不会管value。
注:不用queue和stack是因为其不易遍历。
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status
Description
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.
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.
Output
Please output your list after sorting (format according to sample, pay attention to the spaces,ten spaces need ^ ^).
Sample Input
5
knife qizhou so-so
gun qizhou wonderful
knife zhengzhou good
stick zhengzhou good
rope shengzhou so-so
Sample Output
Case 1
qizhou:
gun wonderful
knife so-so
shengzhou:
rope so-so
zhengzhou:
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是因为其不易遍历。
相关文章推荐
- 算法&模型
- 创建redis集群
- JUC源码分析19-队列-PriorityBlockingQueue
- [Dynamic Language] 用Sphinx自动生成python代码注释文档
- python手记-twisted(3)
- procps工具集 ----Linux中的可用内存指的是什么?
- MongoDB-基础-limit-skip-sort
- 指针的安全转化dyna 4000 mic_cast
- H.264 软/硬编码器 画质量化分析评测
- Hibernate中session的clear(),flush(),evict()方法详解
- Git与GitHub的使用
- HibernateDaoSupport类
- RedisTemplate的使用说明(常用接口方法)
- 上传图片
- ffmpeg的总体认识
- 计算文件夹大小
- 大学总结
- httpclient获取状态码(4.5.2版本)
- Android使用GridView内容填充满不出现滚动的设计
- MongoDB-基础-条件操作符