#K47307. Longest Constructible Word

    ID: 28169 Type: Default 1000ms 256MiB

Longest Constructible Word

Longest Constructible Word

You are given a source word and a list of candidate words. Your task is to select the longest word from the candidate list that can be constructed using the letters of the source word.

Each letter in the source word can only be used as many times as it appears. Formally, for every character c, if freq_word(c) denotes the frequency of c in the candidate word and freq_source(c) denotes its frequency in the source word, then the candidate word is constructible if and only if \[ \forall c,\; freq\_word(c) \leq freq\_source(c). \]

If there are multiple words of the same maximum length, return the one that appears first in the list.

inputFormat

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

  1. The first line contains the source word, a non-empty string.
  2. The second line contains a single integer n which indicates the number of candidate words.
  3. The next n lines each contain one candidate word.

outputFormat

Output to standard output (stdout) a single line containing the longest candidate word that can be constructed using the source word. If no candidate word can be constructed, output an empty line.

## sample
abppplee
5
able
ale
apple
kangaroo
bale
apple

</p>