#C13232. Find Anagram Words

    ID: 42748 Type: Default 1000ms 256MiB

Find Anagram Words

Find Anagram Words

You are given a scrambled string s and a dictionary of words. Your task is to find and output all words from the dictionary that can be obtained by reordering the characters of s. Two words are considered matching if they have exactly the same frequency for each character. In other words, if we denote the frequency of character c in a string str by \( f_{str}(c) \), then word \( w \) is a valid match if and only if \( f_s(c) = f_w(c) \) for all characters c.

If no dictionary word can be formed by reordering s, output nothing.

Note: The order of the output should be the same as the order in the dictionary.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains a string s, the scrambled string.
  • The second line contains an integer n, the number of words in the dictionary.
  • The next n lines each contain one word from the dictionary.

Constraints: The string and dictionary words will consist of printable characters with no spaces.

outputFormat

Output all words that are anagrams of s (i.e. words that can be formed by reordering s) to stdout, each on a separate line. If there are no such words, output nothing.

## sample
act
4
cat
dog
tac
act
cat

tac act

</p>