407177: GYM102697 148 Internet Anagram Server
Description
You have a string, consisting of at most seven characters, not including spaces. You want to find all of the anagrams of the string. A string $$$t$$$ is defined as an anagram of a string $$$s$$$ if an only if the characters of $$$s$$$, in sorted order, not including spaces, are equal to the characters of $$$t$$$, in sorted order, not including spaces.
You're also given a dictionary of $$$n$$$ words.
An anagram is considered a valid anagram if it consists of only space-separated words that are in the dictionary. For example, if the dictionary was the English dictionary, hello and hello world would be valid anagrams, but he llo and helloworld would not be valid anagrams.
Given the string, and the dictionary, print all of the valid anagrams of the string, in alphabetical order.
InputThe first line of input contains a single string $$$s$$$, possibly containing spaces. $$$s$$$ will have less than or equal to seven characters, not including spaces.
The next line of input consists of a single positive integer $$$n$$$, indicating how many words are in the dictionary.
The next $$$n$$$ lines each contain a word not containing spaces: each dictionary entry.
OutputOutput all of the words or phrases that are valid anagrams of the given string, in alphabetical order. Make sure not to repeat words, i.e. all of the answers should be distinct.
ExamplesInputcoderam 12 arm codes dore smac do res mac armcodes blahblah coderam rams codeOutput
arm code code arm coderam dore mac mac doreInput
hello 5 he ll o hel loOutput
he ll o he o ll hel lo ll he o ll o he lo hel o he ll o ll he