sort
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
思路:
题目排序结构为:武器来源,武器名字,武器伤害等级。
来源和名字按字典序排,伤害等级排序为wonderful,good,so-so.
可以把这三个单词用a,b,c表示,然后排序。
sort的compare函数可以这样写
bool compare(point &a,point &b)
{
if(a.name!=b.name)
return a.name<b.name;
if(a.level!=b.level)
return a.level<b.level;
return a.weapon<b.weapon;
}
输出时再把单词换回来,相同的武器来源只输出一次。
AC代码如下:
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
struct point
{
string weapon;
string name;
string level;
} p[500];
bool compare(point &a,point &b)
{
if(a.name!=b.name)
return a.name<b.name;
if(a.level!=b.level)
return a.level<b.level;
//if(a.weapon!=b.weapon)
return a.weapon<b.weapon;
}
//set<string>name;
int main()
{
int n,i,k=0,j;
string weapon,name,level;
while(scanf("%d",&n)!=EOF)
{
for(i=0; i<n; i++)
{
cin>>p[i].weapon>>p[i].name>>p[i].level;
if(p[i].level=="wonderful")
p[i].level="a";
if(p[i].level=="good")
p[i].level="b";
if(p[i].level=="so-so")
p[i].level="c";
}
sort(p,p+n,compare);
printf("Case %d\n",++k);
string s;
for(i=0; i<n; )
{
s=p[i].name;
cout<<s<<":"<<"\n";
for(; p[i].name==s&&i<n; i++)
{
cout<<" "<<p[i].weapon<<" ";
if(p[i].level=="a")
cout<<"wonderful"<<"\n";
if(p[i].level=="b")
cout<<"good"<<"\n";
if(p[i].level=="c")
cout<<"so-so"<<"\n";
}
}
}
return 0;
}