Algorithm
The ship crashed into a reef and is sinking. Now the entire crew must be evacuated. All n crew members have already lined up in a row (for convenience let's label them all from left to right with positive integers from 1 to n) and await further instructions. However, one should evacuate the crew properly, in a strict order. Specifically:
The first crew members to leave the ship are rats. Then women and children (both groups have the same priority) leave the ship. After that all men are evacuated from the ship. The captain leaves the sinking ship last.
If we cannot determine exactly who should leave the ship first for any two members of the crew by the rules from the previous paragraph, then the one who stands to the left in the line leaves the ship first (or in other words, the one whose number in the line is less).
For each crew member we know his status as a crew member, and also his name. All crew members have different names. Determine the order in which to evacuate the crew.
The first line contains an integer n, which is the number of people in the crew (1 ≤ n ≤ 100). Then follow n lines. The i-th of those lines contains two words — the name of the crew member who is i-th in line, and his status on the ship. The words are separated by exactly one space. There are no other spaces in the line. The names consist of Latin letters, the first letter is uppercase, the rest are lowercase. The length of any name is from 1 to 10 characters. The status can have the following values: rat for a rat, woman for a woman, child for a child, man for a man, captain for the captain. The crew contains exactly one captain.
Print n lines. The i-th of them should contain the name of the crew member who must be the i-th one to leave the ship.
6
Jack captain
Alice woman
Charlie man
Teddy rat
Bob child
Julia woman
Teddy
Alice
Bob
Julia
Charlie
Jack
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
#include <iostream>
#include <vector>
int main(){
int n; scanf("%d\n", &n);
std::string line("");
std::string shipCaptain("");
std::vector<std::string> rats;
std::vector<std::string> womenAndChildren;
std::vector<std::string> men;
while(n--){
getline(std::cin, line);
int split = line.find(' ');
std::string name = line.substr(0,split);
std::string title = line.substr(split + 1);
if(title == "captain"){shipCaptain = name;}
else if(title == "rat"){rats.push_back(name);}
else if(title == "woman" || title == "child"){womenAndChildren.push_back(name);}
else{men.push_back(name);}
}
for(int k = 0; k < rats.size(); k++){std::cout << rats[k] << std::endl;}
for(int k = 0; k < womenAndChildren.size(); k++){std::cout << womenAndChildren[k] << std::endl;}
for(int k = 0; k < men.size(); k++){std::cout << men[k] << std::endl;}
std::cout << shipCaptain << std::endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Jack captain
Alice woman
Charlie man
Teddy rat
Bob child
Julia woman
Output
Alice
Bob
Julia
Charlie
Jack
Demonstration
Codeforces Solution-A. Sinking Ship-Solution in C, C++, Java, Python