Stringer
InterviewTime limit1sMemory limit128 MB
Given fixed counts of each of N letters, find the K-th string in alphabetical order among all arrangements, without listing them.
- Level
Medium6 of 10
- Topics
- Combinatorics, Math, Greedy, String matching
- Solved
- No attempts yet
Problem
Consider all strings built from the first letters of the alphabet in which the number of a's, the number of b's, and so on are each fixed to a predetermined (though possibly different) value. List every such string in alphabetical order and number the entries starting from . Given an index , output the -th string in this list.
For example, take the strings over letters (a and b) that use exactly a's and b's. Sorted alphabetically they are:
If , the answer is babab.
Input
The input consists of several datasets. Each dataset spans two lines.
The first line holds two integers and (, ), where is the number of alphabet letters used, is the index of the wanted list entry, and is the total number of strings in the list. The value is not given explicitly in the input.
may be extremely large — far too large to build the whole list — but the input is chosen so that both and fit in a signed 32-bit integer.
The second line holds non-negative integers: the required number of a's, of b's, and so on. Their sum is at least and at most .
The input ends with a line containing two zeros.
Output
For each dataset, print the answer string on its own line. Do not print any extra whitespace, and do not print blank lines between answers.