#C12895. Most Frequent Words

    ID: 42372 Type: Default 1000ms 256MiB

Most Frequent Words

Most Frequent Words

Given a block of text and an integer \( n \), write a program to find the \( n \) most frequent words in the text.

All words should be treated case-insensitively and any punctuation should be ignored. In the event of a tie in frequency, the words must be ordered in lexicographical (alphabetical) order.

The program should read input from stdin and output to stdout. Each output line should contain a word and its frequency, separated by a space.

For example, if the input is:

2
Hello, hello! How are you? You look great, hello you.

Then the expected output is:

hello 3
you 3

inputFormat

The input consists of two lines.

  • The first line is a positive integer \( n \), representing the number of words to output.
  • The second line is a block of text from which words are to be extracted.

outputFormat

Output at most \( n \) lines (if the text has fewer than \( n \) distinct words, output all of them). Each line should contain a word followed by its frequency, separated by a space.

The words must be sorted first by decreasing frequency and then in lexicographical (alphabetical) order in the case of ties.

## sample
2
Hello, hello! How are you? You look great, hello you.
hello 3

you 3

</p>