#C7892. Find Anagrams

    ID: 51813 Type: Default 1000ms 256MiB

Find Anagrams

Find Anagrams

Given a word and a list of words, find all the words in the list that are anagrams of the given word. An anagram of a word is a rearrangement of all its letters. Formally, for a word \( w \), any word \( v \) is an anagram of \( w \) if and only if:

\[ sorted(w) = sorted(v) \]

Your task is to print all the anagrams from the list in the order in which they appear. If no anagrams exist, output an empty line.

inputFormat

The input is read from stdin and follows this format:

  • The first line contains a string, which may include spaces, representing the word to match.
  • The second line contains an integer \( n \), the number of words in the list.
  • Each of the following \( n \) lines contains a word (which may also contain spaces).

outputFormat

Print on a single line (to stdout) all the words from the list that are anagrams of the given word, separated by a single space. If there are no such words, print an empty line.

## sample
listen
4
enlists
google
inlets
banana
inlets

</p>