#K80357. Reorder Logs by Word Frequencies
Reorder Logs by Word Frequencies
Reorder Logs by Word Frequencies
You are given a list of log entries and a mapping of word frequencies. For each log, compute the sum of frequencies of all words in the log. Then, sort the logs in descending order based on this sum. If two logs have the same frequency total, they must appear in the same order as they were in the input.
Note: All logs are non-empty strings consisting of space‐separated words. The frequency mapping may not provide a frequency for every word, in which case the frequency for that word is taken as 0.
The formula for a single log is given by:
\(\text{score}(\ell) = \sum_{w \in \ell} f(w)\)
where \(f(w)\) is the frequency of word \(w\). Your task is to reorder the logs according to their score.
inputFormat
The input is read from standard input (stdin) and has the following format:
- An integer \(n\) representing the number of logs.
- \(n\) lines follow, each line containing a log string.
- An integer \(m\) representing the number of word-frequency pairs.
- \(m\) lines follow; each line contains a word and its frequency (an integer), separated by a space.
outputFormat
Output the reordered logs to standard output (stdout). Each log should be printed on a separate line, in the order determined by their computed frequency score (highest first). Ties must preserve the original input order.
## sample3
error at line 3
server warning
connection timeout
8
error 5
at 1
line 2
3 1
server 4
warning 3
connection 2
timeout 1
error at line 3
server warning
connection timeout
</p>