Sort people by refined class rank, treating missing levels as middle, breaking ties by name.
Easy3SortingStringImplementationInterviewNo attempts yetTime limit2sMemory limit256 MBIn his memoir, the comedian John Cleese calls his father middle-middle-middle-lower-middle class and his mother upper-upper-lower-middle class. Distinctions that fine are hard to keep straight, so write a program that sorts a group of people from the highest class to the lowest.
There are three base classes: upper, middle, and lower. Upper is the highest and lower is the lowest. A base class is split further, so upper-upper is higher than middle-upper, and middle-upper is higher than lower-upper. All of the upper classes (upper-upper, middle-upper, and lower-upper) are still higher than any middle class.
A refined class is refined again, which produces classes such as lower-middle-upper-middle-upper. When you compare two classes and one of them runs out of detail, treat each of its remaining levels as middle. So upper class and middle-upper class are the same class, and so are middle-middle-lower-middle class and lower-middle class.
The first line contains the number of people n (1≤n≤1000).
Each of the next n lines contains a person's name, a colon, a space, and then that person's class. A name is one or more lowercase letters. A class is one or more modifiers followed by the word class, and the colon, the modifiers, and the word class are separated by single spaces. Every modifier is upper, middle, or lower. The last modifier is the base class, and each earlier modifier refines the modifier right after it. No two people share a name. No input line is longer than 256 characters.
Print the n names, one per line, from the highest class to the lowest. Print people whose classes are the same in alphabetical order by name.