Kth Sentence
Time limit3sMemory limit512 MB
Given n distinct words and a length m, print the K-th lexicographically smallest sentence of exactly m letters formed by concatenating the words, or print - if none exists.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Trie, String
- Solved
- No attempts yet
Problem
A student, Kita_masa, is taking an English exam. In this exam, he has to write a sentence of length .
He has completely forgotten English grammar, so he decides to consider every sentence of length that can be built by concatenating the words he knows. He sorts these candidates in lexicographic order and writes the -th one. He believes it must be the correct sentence because is today's lucky number.
Each word may be used multiple times, or not at all. The sentence has no extra characters between words. Two sentences are different if their word order differs, even when the concatenated strings are the same.
Input
The first line contains three integers , , and (, , ), separated by spaces. Each of the next lines contains one word that Kita_masa knows. Each word has a length from 1 to 200 and contains only lowercase letters. All words are distinct.
Output
Print the -th (1-based) sentence of length in lexicographic order. If no such sentence exists, print "-".