#C9593. Count Explained Words
Count Explained Words
Count Explained Words
You are given a dictionary that maps words to their explanations and a list of sentences. Your task is to count, for each sentence, the number of words that have corresponding explanations (i.e. exist as keys in the dictionary).
The input is provided from stdin
and the output must be printed to stdout
. The dictionary is given first, followed by a number of query sentences. Each sentence must be processed independently.
Note: Words in a sentence are separated by whitespace, and matching is case-sensitive.
Mathematical formulation:
For a given sentence S with words \(w_1, w_2, \dots, w_k\) and a dictionary with keys \(D = \{d_1, d_2, \dots, d_m\}\), the answer is computed as:
\[ \text{answer}(S) = \sum_{i=1}^{k} \mathbf{1}_{\{w_i \in D\}} \]inputFormat
The input is read from stdin
and has the following format:
- An integer
D
representing the number of dictionary entries. D
lines follow, each containing a word and its explanation separated by a space. (The explanation may contain spaces, but only the word is used for matching.)- An integer
Q
representing the number of query sentences. Q
lines follow, each representing a sentence. A sentence might be empty.
outputFormat
For each query sentence, print an integer on a new line representing the count of words in that sentence that exist in the dictionary.
## sample5
apple a fruit
banana a fruit
car a vehicle
tree a plant
play an activity
1
I have an apple
1
</p>