Tavan
InterviewTime limit1sMemory limit32 MB
Given a pattern with # placeholders, each replaced by one of K given letters, find the Xth resulting string in alphabetical order.
- Level
Medium6 of 10
- Topics
- Combinatorics, Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
Zeljko was reading his grandma's old letters in the attic and found a word of length . Spilled ink made some of the letters unreadable, so he copied the word onto a sheet of paper and replaced each of the unreadable letters with the character #. His grandma looked at the sheet and gave him different candidate letters for every unreadable position. Zeljko wrote every word that can be built this way in his notebook, and his grandma told him that the word they are looking for is the th one in alphabetical order.
Zeljko had a cold on the day his class learned the alphabet, so he needs your help to find the original word.
Input
The first line contains the integers , , and (, , , ).
The second line contains the word of length that Zeljko wrote on the paper. It consists of lowercase English letters and the character #, and it contains exactly occurrences of #.
Each of the next lines contains one word of length . The th of them consists of the distinct lowercase letters that can replace the th # from the left, and those letters are not necessarily sorted alphabetically.
is always at most the total number of words that can be built.
Output
Print the th word in alphabetical order.
Hint
In the first example the words that can be built are, in alphabetical order: pololjeni, pololjeui, pololjezi, poooljeni, poooljeui, poooljezi, posoljeni, posoljeui, posoljezi.