#P3728. K-th Distinct Subsequence

    ID: 16979 Type: Default 1000ms 256MiB

K-th Distinct Subsequence

K-th Distinct Subsequence

Given a mother sequence S consisting of lowercase letters with length N, consider all its non-empty subsequences. A subsequence is defined as the sequence obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, for S = abc, the subsequences include a, ab, abc, ac, b, bc, and c.

Note that even though the total number of non-empty subsequences is 2N - 1 in theory, duplicate subsequences (i.e. subsequences that are identical in value) should be considered only once. All the distinct subsequences are then sorted in lexicographical (dictionary) order.

Your task is to output the K-th subsequence (1-indexed) in this sorted order.

Note: When writing formulas, use LaTeX format. For example, the total number of non-empty subsequences is given by: \(2^N-1\).

inputFormat

The input consists of two lines. The first line contains the mother sequence S (a string of lowercase letters). The second line contains an integer K (1-indexed), representing the position in the lexicographically sorted list of distinct subsequences that you need to output.

outputFormat

Output a single line containing the K-th distinct subsequence in lexicographical order.

sample

abc
5
b