#C12039. Most Frequent Words from Note Lists

    ID: 41422 Type: Default 1000ms 256MiB

Most Frequent Words from Note Lists

Most Frequent Words from Note Lists

You are given several lists of words. Your task is to find the k most frequent words across all lists. The words should be sorted by their frequency in descending order. In case of a tie, the words should be ordered in lexicographical (alphabetical) order (i.e. \(a < b\)).

Input and Output Format:

The input is read from standard input (stdin) and the output should be printed to standard output (stdout). See the Input/Output section for details.

inputFormat

The input is structured as follows:

  1. An integer \(n\) indicating the number of note lists.
  2. For each note list, a line starting with an integer \(m\) (representing the number of words in this list) followed by \(m\) words separated by spaces.
  3. A final line containing a single integer \(k\), representing the number of most frequent words to output.

Example:

3
3 note1 is good
3 note2 very good
3 note3 is very good
2

outputFormat

Print the \(k\) most frequent words separated by a single space. The words must be sorted primarily by frequency in descending order, and secondarily in lexicographical order for ties.

For the sample input above, the output should be:

good is
## sample
3
3 note1 is good
3 note2 very good
3 note3 is very good
2
good is