#K11551. Frequency Tracker

    ID: 23493 Type: Default 1000ms 256MiB

Frequency Tracker

Frequency Tracker

This problem requires you to implement a frequency tracker that supports two types of operations: adding a word and querying the top \(k\) frequent words. When querying, the words should be sorted by frequency in descending order, with ties broken lexicographically in ascending order. The frequency of a word is defined as the number of times it has been added. For example, adding 'hello' three times gives it a frequency of 3.

Your program must process a sequence of operations provided via standard input and output the result of each query to standard output.

inputFormat

The first line contains an integer n representing the number of operations. The following n lines each contain a command. Each command is either:

  • add <word> — adds the given word to the tracker, or
  • query <k> — queries the top k most frequent words.

outputFormat

For each query command in the input, output a single line containing the k most frequent words separated by a single space. If there are fewer than k words available, output all the words that have been added.

## sample
6
add hello
add world
add hello
query 1
add programming
query 2
hello

hello world

</p>