#K91662. Retrieve Matching Toys

    ID: 38026 Type: Default 1000ms 256MiB

Retrieve Matching Toys

Retrieve Matching Toys

You are given a collection of toys. Each toy is described by an identifier id, a list of tags, and an availability status available.

You will also be provided with search criteria. The criteria may include:

  • A list of tags — the toy must contain all these tags to be a match.
  • An availability flag available — the toy must have the matching availability.

If no criteria are provided, you should return the IDs of all the toys.

Your task is to output the IDs of the toys that match all criteria. The IDs should be printed in the same order as the input, separated by a space.

The filtering conditions can be defined mathematically as follows:

$$\text{Toy is a match} \iff \begin{cases} \forall t \in T_{criteria},\ t \in T_{toy}\\ \text{and}\\ (\text{if } a_{criteria}\text{ is given, then } a_{toy} = a_{criteria}) \end{cases}$$

inputFormat

The input is given from stdin in the following format:

n
id1 available1 num_tags tag1 tag2 ... tag[num_tags]
... (repeat for n toys)
k
[key1] [value_count or value] [values if applicable]
... (repeat for k criteria)

Explanation:

  • The first line contains a single integer n, the number of toys.
  • Each of the next n lines describes a toy with fields:
    • id: a string that identifies the toy.
    • available: either 1 (for available) or 0 (for not available).
    • num_tags: an integer representing the number of tags for the toy.
    • Followed by num_tags strings, each a tag.
  • Next, a line with an integer k (where 0 ≤ k ≤ 2) specifying how many criteria are provided.
  • Each of the next k lines specifies a criterion. The criterion line is formatted as follows:
    • If the key is tags: the line will be: tags m tag1 tag2 ... tag m.
    • If the key is available: the line will be: available x where x is 1 or 0.

outputFormat

Output the matching toy IDs to stdout in one line, separated by a single space.

## sample
4
toy1 1 3 educational battery colorful
toy2 0 2 outdoor sport
toy3 1 2 educational puzzle
toy4 1 2 indoor boardgame
2
tags 1 educational
available 1
toy1 toy3