#K94977. N-th Wonderful Permutation
N-th Wonderful Permutation
N-th Wonderful Permutation
You are given an integer n and a string s of length L. Your task is to find the n-th lexicographically smallest wonderful permutation of the characters of s. A permutation is called wonderful if no two consecutive characters are the same.
Formally, let \(P\) be a permutation of characters of \(s\). The permutation \(P = p_1p_2...p_L\) is wonderful if for all \(1 \le i < L\), \(p_i \neq p_{i+1}\). All such wonderful permutations are then sorted in lexicographical order. If there are fewer than n wonderful permutations, output "Impossible".
Note: The parameter n is 1-indexed.
inputFormat
The input is read from standard input and consists of two lines:
- The first line contains two integers n and L, where n denotes the rank of the permutation to retrieve and L denotes the length of the string.
- The second line contains the string s of length L, consisting of lowercase letters.
outputFormat
Output to standard output a single line containing the n-th lexicographically smallest wonderful permutation of s. If no such permutation exists, print Impossible
.
3 3
abc
bac