#K38282. Calculate Similarity Scores
Calculate Similarity Scores
Calculate Similarity Scores
You are given a collection of category descriptions and a list of keywords. Your task is to compute the similarity score between pairs of categories based on the frequencies of the given keywords. For any two categories with descriptions, the similarity score is defined as
$$score(a,b)=\sum_{i=1}^{m}|f_a(k_i)-f_b(k_i)|,$$
where \(f_a(k_i)\) denotes the number of times the keyword \(k_i\) appears in the description of category a, \(m\) denotes the total number of keywords, and the categories are 1-indexed. Each query provides two category indices, and you must compute and output their similarity score.
inputFormat
The input is given in the following format:
- An integer \(n\) denoting the number of category descriptions.
- \(n\) lines follow, each containing a category description (a string).
- An integer \(m\) denoting the number of keywords.
- \(m\) lines follow, each containing a single keyword.
- An integer \(q\) denoting the number of queries.
- \(q\) lines follow, each containing two space-separated integers \(a\) and \(b\) (1-indexed) representing the categories to be compared.
outputFormat
For each query, output the similarity score on a separate line.
## sample3
mobile phone accessories
laptop accessories
mobile laptop and accessories
3
mobile
laptop
accessories
2
1 2
2 3
2
1
</p>