#C6715. Sort Book Titles Ignoring Leading Articles

    ID: 50506 Type: Default 1000ms 256MiB

Sort Book Titles Ignoring Leading Articles

Sort Book Titles Ignoring Leading Articles

You are given a list of book titles. Your task is to sort these titles in alphabetical order ignoring any leading articles. The articles you must ignore are "a", "an", and "the" (case insensitive). For example, the title "The Great Gatsby" should be treated as "Great Gatsby" when sorting, but its original form should be output.

Note: When removing the article, only the first occurrence if it appears at the beginning of the title should be removed. The sorted order must preserve the original title format.

Hint: You may find it useful to compare titles using a custom key which removes the leading article (if present) and converts the rest of the title to lowercase.

Mathematical formulation:

Let \(f(s)\) be defined as: \[ f(s)=\begin{cases} \text{lower}(s[len]) & \text{if } s \text{ starts with } \texttt{a },\texttt{an } or \texttt{the } \\ \text{lower}(s) & \text{otherwise} \end{cases} \] Then, you are to sort the list of titles \(\{s_1, s_2, \ldots, s_n\}\) based on \(f(s_i)\) in lexicographical order.

inputFormat

The input is read from standard input (stdin) and has the following format:

<integer t>
<title 1>
<title 2>
...
<title t>

Where:

  • t is the number of book titles.
  • Each of the following t lines contains one book title as a string.

outputFormat

Output the sorted list of book titles, one title per line, to the standard output (stdout). The order should be determined by ignoring any leading articles "a", "an", or "the".

## sample
4
The Great Gatsby
A Tale of Two Cities
The Catcher in the Rye
Moby Dick
The Catcher in the Rye

The Great Gatsby Moby Dick A Tale of Two Cities

</p>