407177: GYM102697 148 Internet Anagram Server

Memory Limit:256 MB Time Limit:15 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

148. Internet Anagram Servertime limit per test15 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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.

Input

The 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.

Output

Output 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.

ExamplesInput
coderam
12
arm
codes
dore
smac
do
res
mac
armcodes
blahblah
coderam
rams
code
Output
arm code
code arm
coderam
dore mac
mac dore
Input
hello
5
he
ll
o
hel
lo
Output
he ll o
he o ll
hel lo
ll he o
ll o he
lo hel
o he ll
o ll he

加入题单

算法标签: