#C4849. Top K Frequent Search Queries
Top K Frequent Search Queries
Top K Frequent Search Queries
You are given several test cases. For each test case, you are given a list of search queries and an integer \(k\). Your task is to find the \(k\) most frequent queries from the list and output them as a space-separated string in descending order of their frequency.
If two or more queries have the same frequency, you may output them in any order (but a consistent order is recommended, for example, the lexicographical order or the order of first appearance).
Example:
Input: 2 6 2 apple banana apple orange banana apple 8 3 query1 query2 query1 query1 query3 query2 query2 query4</p>Output: apple banana query1 query2 query3
In the first test case, "apple" appears 3 times, "banana" 2 times and "orange" 1 time. Therefore, the top 2 frequent queries are "apple" and "banana".
inputFormat
The input is read from stdin and has the following format:
T n1 k1 query1 query2 ... query_n1 n2 k2 query1 query2 ... query_n2 ... nT kT query1 query2 ... query_nT
Here, \(T\) is the number of test cases. For each test case, the first line contains two integers: \(n\) (the number of queries) and \(k\) (the number of top queries to output). The next line contains \(n\) strings representing the search queries, separated by spaces.
outputFormat
For each test case, output a single line containing the \(k\) most frequent queries separated by a single space. The output should be written to stdout.
## sample2
6 2
apple banana apple orange banana apple
8 3
query1 query2 query1 query1 query3 query2 query2 query4
apple banana
query1 query2 query3
</p>