Suppose i have a dictionary of an alphabet D (|D|>1) and i have an integer n (n between 1 and 1e18). I want to find the nth lexicographical string in this dictionary. How can I solve this problem?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
Suppose i have a dictionary of an alphabet D (|D|>1) and i have an integer n (n between 1 and 1e18). I want to find the nth lexicographical string in this dictionary. How can I solve this problem?
Name |
---|
Suppose there are $$$x$$$ distinct characters, then we can simply transform $$$n$$$ into base $$$x$$$ to get the answer.
let D = {A,B}. let's say I want to find 2nd (=x) number in this dictionary. If I convert '2' to base 2 then we get '10'. Now how do we generate the string ?
.
As far as I understand bases, 1 and 0 are the indexes. The indexes also represent the weights, hence lexicography. So the string corresponding to 10 is BA.
walk on A-ary trie by holding subtree size. Where A is Alphabet size
If this problem is about finding the
n
th lexicographical string, then the answer is character A n times, where A is the smallest lexicographical. I think they intended for the sequence to be sorted by length, and only then lexicographically. Then you can first find its length, denote it asL
, then findn
th lexicographical string of lengthL
(n
will be different after canceling out every string with size <L
). I can share the code if you want