#K38282. Calculate Similarity Scores

    ID: 26164 Type: Default 1000ms 256MiB

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:

  1. An integer \(n\) denoting the number of category descriptions.
  2. \(n\) lines follow, each containing a category description (a string).
  3. An integer \(m\) denoting the number of keywords.
  4. \(m\) lines follow, each containing a single keyword.
  5. An integer \(q\) denoting the number of queries.
  6. \(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.

## sample
3
mobile phone accessories
laptop accessories
mobile laptop and accessories
3
mobile
laptop
accessories
2
1 2
2 3
2

1

</p>