#K36447. Filter Products By Attributes
Filter Products By Attributes
Filter Products By Attributes
Given a list of products, each with a unique ID and a list of attributes, your task is to filter the products based on a set of selected attributes. A product is considered valid if and only if it contains all of the selected attributes. You should output the IDs of all valid products in the order they appear in the input.
The problem can be formalized as follows: Given a product \( p \) with an attribute set \( A_p \) and a set of selected attributes \( S \), the product is valid if \( S \subseteq A_p \). Your program should implement this logic.
Input Format: The input is given via standard input (stdin) and has the following structure:
- An integer \(N\) representing the number of products.
- Then \(N\) lines follow where each line describes a product. Each product line starts with an integer ID, followed by an integer \(K\) (the number of attributes), then \(K\) space-separated attribute strings.
- An integer \(M\) representing the number of selected attributes.
- If \(M > 0\), a line with \(M\) space-separated strings representing the selected attributes.
Output Format: Output a single line to standard output (stdout) containing the IDs of valid products separated by a space. If no products match the criteria, output an empty line.
inputFormat
The input is read from standard input (stdin) in the following format:
- First line: an integer \(N\), the number of products.
- Next \(N\) lines: each line contains a product description starting with the product ID, then an integer \(K\) (number of attributes), followed by \(K\) attribute strings.
- Next line: an integer \(M\), the number of selected attributes.
- If \(M > 0\), the next line contains \(M\) space-separated attribute strings.
outputFormat
Output a single line containing the IDs of products (in the same order as given in input) that have all the selected attributes. The IDs should be separated by a space. If no product meets the criteria, output an empty line.
## sample3
1 3 red large cotton
2 3 blue small silk
3 3 red small cotton
2
red cotton
1 3