HDU 3293 由简单排序引发出的一点思考

来源:互联网 发布:淘宝产品卖点 编辑:程序博客网 时间:2024/05/19 09:36


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

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