Under construction

Still being built. Things may change

Memorizing English Words Is Painful

Interview

Time limit1sMemory limit1024 MB

Summary
Count each word of length at least M, then sort the distinct words by frequency descending, length descending, and alphabetical order.
Level

Medium4 of 10

Topics
Hash map, Sorting, String, Implementation
Solved
No attempts yet

Problem

Hwa-eun wants to memorize English words based on the questions she got wrong on her English exam. She is going to make a vocabulary notebook to memorize English words efficiently. The order of words in the notebook is determined by applying the following priorities in order.

  1. Words that appear more often come first.
  2. Words with greater length come first.
  3. Words that come earlier in alphabetical order come first.

Words shorter than MM can be memorized just by reading them, so she only memorizes words of length at least MM. Let us make a vocabulary notebook so that Hwa-eun can memorize English words efficiently.

Input

The first line gives the number of words NN in the English passage and the length threshold MM for words to memorize, separated by a space. (1≤N≤100 0001 \leq N \leq 100\,000, 1≤M≤101 \leq M \leq 10)

From the second line to the N+1N+1-th line, the words to memorize are given. Each word consists only of lowercase English letters and its length does not exceed 1010.

Only inputs for which the notebook contains at least one word are given.

Output

Print the words in Hwa-eun's notebook in order, starting from the word at the front of the notebook, one word per line.

Hint

You can make input and output faster by doing the following.

  • If you use C++ and want to use cin/cout, apply both cin.tie(NULL) and sync_with_stdio(false), and use the newline character (\n) instead of endl. However, once you do this, you must no longer use C input/output functions such as scanf/printf/puts/getchar/putchar.
  • If you use Java, you can use BufferedReader and BufferedWriter instead of Scanner and System.out.println. Call BufferedWriter.flush only once, at the very end.
  • If you use Python, you can use sys.stdin.readline instead of input. However, since this reads the trailing newline character as well, it is better to add .rstrip() when you want to store the string. Finally, Pypy3 has the same syntax as Python 3 and generally runs faster. If you want to use Python on a problem with a large amount of computation, submitting with Pypy is recommended.

Examples2

  1. Example 1

    Input
    7 4
    apple
    ant
    sand
    apple
    append
    sand
    sand
    
    Expected output
    sand
    apple
    append
    
  2. Example 2

    Input
    12 5
    appearance
    append
    attendance
    swim
    swift
    swift
    swift
    mouse
    wallet
    mouse
    ice
    age
    
    Expected output
    swift
    mouse
    appearance
    attendance
    append
    wallet