#P3025. Bessie's Forgotten Password

    ID: 16283 Type: Default 1000ms 256MiB

Bessie's Forgotten Password

Bessie's Forgotten Password

Bessie forgot her cowtube password! She remembers that her password, denoted by \(P\), is a string of length \(L\) (where \(1 \leq L \leq 1000\)) and is made by concatenating one or more words from a dictionary of \(NW\) unique words. Each word in the dictionary is a sequence of 1 to 20 lowercase letters ('a' to 'z').

In addition, Bessie recalls some letters in the password at specific positions; the unknown letters are represented by a '?' character. The password must be formed by a concatenation of dictionary words such that the known characters in the pattern are matched exactly. If there is more than one valid password, output the lexicographically smallest one.

For example, if Bessie remembers her password as a??l?ban??????? and the dictionary contains the words:
apple
cow
farmer
banana
bananas
pies
then the two possible passwords are applebananapies and applebananascow. Since the former is lexicographically smaller, it should be returned.

Input Constraints:
\(1 \leq L \leq 1000\), \(1 \leq NW \leq 1000\).

inputFormat

The first line contains two integers \(L\) and \(NW\) separated by a space.

The second line contains the password pattern, a string of length \(L\) consisting of lowercase letters and '?' characters.

The following \(NW\) lines each contain one dictionary word.

outputFormat

Output the valid password that Bessie could have used. If multiple passwords are valid, output the lexicographically smallest one.

sample

15 6
a??l?ban???????
apple
cow
farmer
banana
bananas
pies
applebananapies