1348C.md (2896B)
1 Source: https://codeforces.com/problemset/problem/1348/C 2 3 C. Phoenix and Distribution 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Phoenix has a string $s$ consisting of lowercase Latin letters. He wants to distribute all the letters of his string into $k$ non-empty strings $a_1, a_2, \dots, a_k$ such that every letter of $s$ goes to exactly one of the strings $a_i$. The strings $a_i$ do not need to be substrings of $s$. Phoenix can distribute letters of $s$ and rearrange the letters within each string $a_i$ however he wants. 22 23 For example, if $s = $ baba and $k=2$, Phoenix may distribute the letters of his string in many ways, such as: 24 25 * ba and ba 26 * a and abb 27 * ab and ab 28 * aa and bb 29 30 But these ways are invalid: 31 32 * baa and ba 33 * b and ba 34 * baba and empty string ($a_i$ should be non-empty) 35 36 Phoenix wants to distribute the letters of his string $s$ into $k$ strings $a_1, a_2, \dots, a_k$ to minimize the lexicographically maximum string among them, i. e. minimize $max(a_1, a_2, \dots, a_k)$. Help him find the optimal distribution and print the minimal possible value of $max(a_1, a_2, \dots, a_k)$. 37 38 String $x$ is lexicographically less than string $y$ if either $x$ is a prefix of $y$ and $x \ne y$, or there exists an index $i$ ($1 \le i \le min(|x|, |y|))$ such that $x_i$ < $y_i$ and for every $j$ $(1 \le j \lt i)$ $x_j = y_j$. Here $|x|$ denotes the length of the string $x$. 39 40 Input 41 42 The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. 43 44 The first line of each test case consists of two integers $n$ and $k$ ($1 \le k \le n \le 10^5$) — the length of string $s$ and the number of non-empty strings, into which Phoenix wants to distribute letters of $s$, respectively. 45 46 The second line of each test case contains a string $s$ of length $n$ consisting only of lowercase Latin letters. 47 48 It is guaranteed that the sum of $n$ over all test cases is $\le 10^5$. 49 50 Output 51 52 Print $t$ answers — one per test case. The $i$-th answer should be the minimal possible value of $max(a_1, a_2, \dots, a_k)$ in the $i$-th test case. 53 54 Example 55 56 Input 57 58 ``` 59 6 60 4 2 61 baba 62 5 2 63 baacb 64 5 3 65 baacb 66 5 3 67 aaaaa 68 6 4 69 aaxxzz 70 7 1 71 phoenix 72 ``` 73 74 Output 75 76 ``` 77 ab 78 abbc 79 b 80 aa 81 x 82 ehinopx 83 ``` 84 85 Note 86 87 In the first test case, one optimal solution is to distribute baba into ab and ab. 88 89 In the second test case, one optimal solution is to distribute baacb into abbc and a. 90 91 In the third test case, one optimal solution is to distribute baacb into ac, ab, and b. 92 93 In the fourth test case, one optimal solution is to distribute aaaaa into aa, aa, and a. 94 95 In the fifth test case, one optimal solution is to distribute aaxxzz into az, az, x, and x. 96 97 In the sixth test case, one optimal solution is to distribute phoenix into ehinopx.