n 个小朋友在玩游戏,编号依次为 1∼n。
每个小朋友都有一个身份,为 rat(老鼠)、woman(女人)、child(孩子)、man(男人)、captain(舰长) ######之一。
已知,这几种身份的优先级是有区别的:
rat 为第一优先级。
woman 和 child 为第二优先级。
man 为第三优先级。
captain 为第四优先级。
现在,要给小朋友们排队,排队的规则为:
对于不同优先级的小朋友,优先级越靠前(越小)则在队伍中的位置越靠前。
对于同一优先级的小朋友,编号越小则在队伍中的位置越靠前。
请你确定小朋友们的具体排队顺序。
输入格式
第一行包含整数 n。
接下来 n 行,其中第 i 行用来描述编号为 i ######的小朋友,首先包含一个字符串,表示小朋友的名字,然后包含一个字符串,表示他的身份。
每个小朋友的名字都是一个长度范围在 [1,10] 的由英文字母构成的字符串,其中首字母大写,其余小写。
小朋友的名字两两不同。
表示身份的字符串一定是 rat、woman、child、man、captain 之一。
保证身份为 captain 的小朋友有且仅有一个。
输出格式
共 n 行,其中第 i 行输出排在队伍中第 i 位的小朋友的名字。
数据范围
前 4 个测试点满足 1≤n≤10。
所有测试点满足 1≤n≤100。
输入样例:
6
Jack captain
Alice woman
Charlie man
Teddy rat
Bob child
Julia woman
输出样例:
Teddy
Alice
Bob
Julia
Charlie
Jack
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = 110;
unordered_map<string, int> mp;
struct node{
string name, id;
int idx;
bool operator < (const node & w) const{
if(mp[id] != mp[w.id]) return mp[id] < mp[w.id];
else return idx < w.idx;
}
}a[N];
int main (){
int n;
cin >> n;
mp["rat"] = 1, mp["woman"] = mp["child"] = 2, mp["man"] = 3, mp["captain"] = 4;
for(int i = 0; i < n; i ++){
string name, id;
cin >> name >> id;
a[i] = {name, id, i};
}
sort(a, a + n);
for(int i = 0; i < n; i ++){
cout << a[i].name << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef pair<string, pair<string, int>> PSSI;
unordered_map<string,int> mp;
int n;
int main (){
cin >> n;
vector<PSSI> res;
mp["rat"] = 1, mp["woman"] = mp["child"] = 2, mp["man"] = 3, mp["captain"] = 4;
for(int i = 0; i < n; i ++){
string name, id;
cin >> name >> id;
res.push_back({name, {id, i}});
}
sort(res.begin(), res.end(), [&](PSSI &a, PSSI &b){
if(mp[a.second.first] != mp[b.second.first]) return mp[a.second.first] < mp[b.second.first];
else return a.second.second < b.second.second;
});
for(int i = 0; i < n; i ++) cout << res[i].first << endl;
return 0;
}