#C14100. Keyword Frequency Counter
Keyword Frequency Counter
Keyword Frequency Counter
You are given several text documents and a list of keywords. Your task is to count the occurrences of each keyword (exact match, case-sensitive) over all documents and then output the top N keywords based on their frequency. When two keywords have the same frequency, preserve the order in which they appear in the keyword list.
The input is given through stdin and the output should be printed to stdout. The output consists of N lines, each line containing a keyword and its count, separated by a space.
The frequency of a keyword \(k\) is defined as the total number of times \(k\) appears in all documents. Formally, if you have \(F\) documents and \(W_{ij}\) represents the j-th word in the i-th document, then the frequency is:
[ \text{count}(k) = \sum_{i=1}^{F} \sum_{j} I{W_{ij} = k} ]
You must implement a solution that reads from stdin
and outputs to stdout
following the input/output specifications below.
inputFormat
The input consists of several parts:
- An integer \(F\) representing the number of text documents.
- \(F\) lines follow, each line representing the content of a document. Each document is a space-separated string of words.
- An integer \(K\) representing the number of keywords.
- A single line containing \(K\) space-separated keywords.
- An integer \(N\) representing the number of top keywords to output.
Constraints:
- 1 ≤ F, K, N ≤ 1000
- Words consist of alphanumeric characters.
outputFormat
Output exactly \(N\) lines to stdout
. Each line should contain a keyword and its corresponding count, separated by a space. The keywords should be listed in descending order of frequency. If two keywords have the same frequency, the keyword that appears earlier in the input keyword list should come first.
2
python code developer python code\ndeveloper code python code python
3
python code developer
3
python 4
code 4
developer 2
</p>