#K33897. Count Flower Patterns
Count Flower Patterns
Count Flower Patterns
Given a database of flower patterns and a list of query patterns, your task is to count the number of times each query appears exactly in the database. The problem is straightforward: first you are given an integer n denoting the number of flower patterns, followed by n strings, each representing a flower pattern. Then an integer q is given, followed by q query strings. For each query, print the number of occurrences in the database.
The solution should read from standard input (stdin) and write the result to standard output (stdout). Each output should be printed on a separate line.
Note: Use efficient data structures (such as hash maps) to achieve optimal performance. The matching should be done exactly (case-sensitive) and there is no partial matching or pattern search involved.
Formally, if we denote the database as \(D = \{d_1, d_2, \dots, d_n\}\) and the queries as \(Q = \{q_1, q_2, \dots, q_q\}\), for each \(q_i\) you need to compute:
\[ ans_i = \# \{ j \; | \; d_j = q_i \} \]inputFormat
The input is read from stdin and has the following format:
pattern_1 pattern_2 ... pattern_n query_1 query_2 ... query_q
Where:
n
is the number of flower patterns.pattern_i
(for \(1 \le i \le n\)) is a string that represents a flower pattern.q
is the number of queries.query_i
(for \(1 \le i \le q\)) is a query string for which you must count the occurrences in the database.
outputFormat
The output should be written to stdout and consist of exactly q
lines. The i-th line should contain the count of the number of times query_i
appears in the database.
5
flower
cactus
rose
daisy
tulip
3
cactus
fern
tulip
1
0
1
</p>