#K54387. Word Frequency Counter and Formatter

    ID: 29742 Type: Default 1000ms 256MiB

Word Frequency Counter and Formatter

Word Frequency Counter and Formatter

You are given a collection of N pages, where each page is a string of words separated by spaces. For each page, your task is to count the frequency of each word and then output the results in a specific format. The words for each page should be sorted in descending order of their frequency, and in case of a tie, they should be sorted in lexicographical (alphabetical) order.

The output for each page should consist of one word-frequency pair per line, formatted as word:frequency. Separate the outputs for different pages with an empty line.

Mathematically, if the frequency of a word is denoted by \( f(word) \), then the sorting order is defined by:

\( (a, f(a)) \succ (b, f(b)) \iff (f(a) > f(b)) \text{ or } (f(a) = f(b) \text{ and } a < b) \).

Implement a program that reads the input from stdin and prints the formatted result to stdout.

inputFormat

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

N
page_1
page_2
...
page_N

Where:

  • N is an integer representing the number of pages.
  • Each page_i is a single line of text containing a sequence of words separated by spaces.

outputFormat

For each page, output the list of word-frequency pairs on a new line in the format word:frequency. Words must be sorted primarily by decreasing frequency and secondarily in lexicographical order if frequencies are equal. Separate the outputs of different pages by an empty line. The output should be printed to stdout.

## sample
1
hello world hello
hello:2

world:1

</p>