#C12490. Sort Strings by Length

    ID: 41923 Type: Default 1000ms 256MiB

Sort Strings by Length

Sort Strings by Length

You are given a list of strings. Your task is to sort the list according to the following rules:

  • First, sort by the length of the strings in descending order.
  • If two strings have the same length, sort them in lexicographical ascending order.

Mathematically, for two strings \(a\) and \(b\), the comparator can be defined as:

\[ \text{Compare}(a, b) = \begin{cases} -1, & \text{if } |a| > |b| \\ \text{lexicographical comparison}, & \text{if } |a| = |b| \\ 1, & \text{if } |a| < |b| \end{cases} \]

For example, if the input strings are "apple", "orange", "fig", "pineapple", "lime", then the correct output is:

pineapple orange apple lime fig

inputFormat

The input is read from stdin in the following format:

  1. The first line contains an integer \(n\) indicating the number of strings.
  2. The following \(n\) lines each contain one string.

outputFormat

Output the sorted list of strings in a single line to stdout, with each string separated by a single space.

## sample
5
apple
orange
fig
pineapple
lime
pineapple orange apple lime fig