#C11726. Tag Association Counting

    ID: 41074 Type: Default 1000ms 256MiB

Tag Association Counting

Tag Association Counting

You are given a list of posts where each post contains a unique identifier followed by one or more tags. Your task is to count the number of posts associated with each query tag. A post is considered to be associated with a tag if that tag appears in its list.

The input begins with an integer n representing the number of posts. The following n lines each contain a post identifier and several tags (separated by spaces). After these, an integer q is given, followed by q lines, each containing a query tag. For each query tag, output the count of posts that contain that tag.

Note: In each post, if a tag appears more than once, it is counted only once. Tag matching is case-sensitive.

The mathematical formulation can be represented in \( \textbf{O}(n + q) \) time by processing each post and each query efficiently.

inputFormat

The first line contains an integer n (1 ≤ n ≤ 105), the number of posts.

The next n lines each contain a post identifier and a list of tags separated by spaces.

The following line contains an integer q (1 ≤ q ≤ 105), the number of query tags.

The next q lines each contain a single tag to be queried.

outputFormat

Output a single line with q space-separated integers. Each integer is the number of posts associated with the corresponding query tag.

## sample
5
post1 tag1 tag2 tag3
post2 tag1 tag3
post3 tag2
post4 tag3 tag2 tag1
post5 tag4
3
tag1
tag2
tag3
3 3 3