#C12668. Filter and Sort Emails

    ID: 42120 Type: Default 1000ms 256MiB

Filter and Sort Emails

Filter and Sort Emails

You are given a list of email addresses and a list of allowed domains. Your task is to filter the emails so that only those whose domain (the part after the '@' symbol) is present in the allowed domains are retained. Then, sort the remaining email addresses in alphabetical order.

Note: The domain of an email is defined as the substring after the '@' character. For example, in the email bob@gmail.com, the domain is gmail.com. The sorting order should follow the usual lexicographical order.

The problem can be formally described as follows: Let \(E = [e_1, e_2, \dots, e_n]\) be the list of email addresses and \(D = [d_1, d_2, \dots, d_m]\) be the list of allowed domains. You need to find the set \(S = \{ e \in E : \text{domain}(e) \in D \}\) and output the emails in \(S\) sorted in increasing lexicographical order.

inputFormat

The first line of input contains two space-separated integers \(n\) and \(m\), where \(n\) is the number of email addresses and \(m\) is the number of allowed domains.

This is followed by \(n\) lines, each containing one email address.

Then, there are \(m\) lines, each containing one allowed domain.

outputFormat

Output the filtered email addresses sorted in alphabetical order, each on a separate line. If no email address meets the criteria, output nothing (i.e. an empty output).

## sample
5 2
bob@gmail.com
alice@yahoo.com
carol@gmail.com
dave@hotmail.com
eve@gmail.com
gmail.com
hotmail.com
bob@gmail.com

carol@gmail.com dave@hotmail.com eve@gmail.com

</p>