#C8588. Most Frequent Strings

    ID: 52586 Type: Default 1000ms 256MiB

Most Frequent Strings

Most Frequent Strings

You are given a list of strings and an integer k. Your task is to find the top k most frequent strings in the list. If two strings have the same frequency, they are sorted in lexicographical order.

If the input list is empty, output "No strings to analyze".

The sorting criteria can be expressed as follows:

$$(\text{frequency}, \text{string}) \rightarrow ( -\text{frequency}, \text{string} ) $$

For example:

  • Input: ["apple", "banana", "apple", "orange", "banana", "apple"] with k = 2, output should be: "apple banana".
  • Input: ["apple", "banana", "cherry"] with k = 5, output should be: "apple banana cherry".
  • Input: [] with k = 2, output should be: "No strings to analyze".

inputFormat

The input is given via standard input (stdin) in the following format:

  1. An integer n representing the number of strings.
  2. n lines each containing one string.
  3. An integer k specifying how many of the most frequent strings to output.

Note: If n is 0, then there will be no strings and only the integer k will follow.

outputFormat

Output the k most frequent strings separated by a single space (for the case when the list is non-empty). If the list is empty, output exactly "No strings to analyze".

The output should be sent to standard output (stdout).

## sample
6
apple
banana
apple
orange
banana
apple
2
apple banana