C - One More aab aba baa 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 300300

問題文

文字列 SS の各文字を並べ替えて作ることが可能な文字列を辞書順にすべて列挙したとき、前から KK 番目にくる文字列を求めてください。

「各文字を並べ替えて作ることが可能な文字列」とは? 「文字列 AA が文字列 BB の各文字を並べ替えて作ることが可能な文字列である」とは、任意の文字が文字列 AA と文字列 BB に同数含まれるということを指します。

制約

  • 1S81 \le |S| \le 8
  • SS は英小文字のみからなる
  • SS の各文字を並べ替えてできる文字列は KK 種類以上存在する

入力

入力は以下の形式で標準入力から与えられる。

SS KK

出力

答えを出力せよ。


入力例 1 Copy

Copy
aab 2

出力例 1 Copy

Copy
aba

文字列 aab の各文字を並べ替えて作ることが可能な文字列は {\{ aab, aba, baa }\}33 つですが、このうち辞書順で前から 22 番目にくるものは aba です。


入力例 2 Copy

Copy
baba 4

出力例 2 Copy

Copy
baab

入力例 3 Copy

Copy
ydxwacbz 40320

出力例 3 Copy

Copy
zyxwdcba

Score : 300300 points

Problem Statement

Find the KK-th lexicographically smallest string among the strings that are permutations of a string SS.

What is a permutation of a string?A string AA is said to be a permutation of a string BB when any character occurs the same number of times in AA and BB.

Constraints

  • 1S81 \le |S| \le 8
  • SS consists of lowercase English letters.
  • There are at least KK distinct strings that are permutations of SS.

Input

Input is given from Standard Input in the following format:

SS KK

Output

Print the answer.


Sample Input 1 Copy

Copy
aab 2

Sample Output 1 Copy

Copy
aba

There are three permutations of a string aab: {\{ aab, aba, baa }\}. The 22-nd lexicographically smallest of them is aba.


Sample Input 2 Copy

Copy
baba 4

Sample Output 2 Copy

Copy
baab

Sample Input 3 Copy

Copy
ydxwacbz 40320

Sample Output 3 Copy

Copy
zyxwdcba


2022-05-08 (日)
17:38:55 +00:00