#K86842. Word Frequency Analysis

    ID: 36954 Type: Default 1000ms 256MiB

Word Frequency Analysis

Word Frequency Analysis

You are given an integer k and a paragraph of text. Your task is to count the frequency of each word in the paragraph after normalizing the text (i.e., converting all letters to lowercase and removing punctuation). Then, output the k words with the highest frequencies.

The words should be sorted by their frequency in descending order. If two words have the same frequency, they should be ordered in alphabetical order (case insensitive). In other words, you need to sort the words by $$\big(-frequency,\; word\big)$$ order.

Note: The input text may contain punctuation marks. All non-alphanumeric characters should be treated as delimiters.

For example, if k = 3 and the paragraph is "Hello world! This is a test. Hello World! Hello everyone, hello.", then the output should be:

hello 4
world 2
a 1

inputFormat

The input is provided via standard input (stdin) and consists of two lines:

  1. The first line contains a single integer k, which represents the number of top frequent words to output.
  2. The second line contains the paragraph of text.

You can assume that the paragraph fits in memory.

outputFormat

Output the top k words and their frequencies to standard output (stdout). Each line of the output should contain a word and its frequency separated by a single space.

## sample
3
Hello world! This is a test. Hello World! Hello everyone, hello.
hello 4

world 2 a 1

</p>