Phoenix has a string ss consisting of lowercase Latin letters. He wants to distribute all the letters of his string into kk non-empty strings a1,a2,…,aka1,a2,…,ak such that every letter of ss goes to exactly one of the strings aiai. The strings aiai do not need to be substrings of ss. Phoenix can distribute letters of ss and rearrange the letters within each string aiai however he wants.
For example, if s=s= baba and k=2k=2, Phoenix may distribute the letters of his string in many ways, such as:
- ba and ba
- a and abb
- ab and ab
- aa and bb
But these ways are invalid:
- baa and ba
- b and ba
- baba and empty string (aiai should be non-empty)
Phoenix wants to distribute the letters of his string ss into kk strings a1,a2,…,aka1,a2,…,ak to minimize the lexicographically maximum string among them, i. e. minimize max(a1,a2,…,ak)max(a1,a2,…,ak). Help him find the optimal distribution and print the minimal possible value of max(a1,a2,…,ak)max(a1,a2,…,ak).
String xx is lexicographically less than string yy if either xx is a prefix of yy and x≠yx≠y, or there exists an index ii (1≤i≤min(|x|,|y|))1≤i≤min(|x|,|y|)) such that xixi < yiyi and for every jj (1≤j<i)(1≤j<i) xj=yjxj=yj. Here |x||x| denotes the length of the string xx.
Input
The input consists of multiple test cases. The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Each test case consists of two lines.
The first line of each test case consists of two integers nn and kk (1≤k≤n≤1051≤k≤n≤105) — the length of string ss and the number of non-empty strings, into which Phoenix wants to distribute letters of ss, respectively.
The second line of each test case contains a string ss of length nn consisting only of lowercase Latin letters.
It is guaranteed that the sum of nn over all test cases is ≤105≤105.
Output
Print tt answers — one per test case. The ii-th answer should be the minimal possible value of max(a1,a2,…,ak)max(a1,a2,…,ak) in the ii-th test case.
Example
input
6 4 2 baba 5 2 baacb 5 3 baacb 5 3 aaaaa 6 4 aaxxzz 7 1 phoenix
output
ab abbc b aa x ehinopx
题意:
将一个长度为n的字符串按任意顺序分成k份,最小化k份中的字典序最大字符串。
- 最理想的最小化最大字符串为 k = 1时,即:abcdefgh…… 等,即按顺序排列,这样可以让最大的字符放在后面。
- 若k = 1时,则把b抽出,则b是最大的字符串。
- 若k = 2时,则把b和c抽出来,则,c是最大的字符串。
以上总结的是一种特殊情况,即s[0] != s[k-1]时,比如k = 4时,s = “ abbbdefgggi ”。s可分为:
“adefgggi” , “b”,"b","b"。则最大的是 “b”, 则只要当排好序的s中s[0] != s[k-1],则答案就是s[k-1]。
当s[0] == s[k-1],即最小的字符 >=k 个时(如k = 3):
- s[k] == s[n-1] : s = “ aaabbbbbbb”,字符串会被分为:“abb”,"abb","abbb",一开始三个字符串都平分,分别隔3个字符取一份,不让某一个字符串偏大,即都分的“abb”,后多余的给到的字符串就是最大的字符串即:“abbb”。
- s[k] != s[n-1] : s = "aaaabbbbbbbc",这时,首要任务就是让c最小化,即把c放在最后面,因此在平分为3份时,取2个字符各为一份即 "a"和"a"(一定能取到k-1个最小字符,因为s[0] == s[k-1]),最后一个字符串就是最大的,即"aabbbbbbbc"。
代码如下:
#include <iostream>
#include <algorithm>
using namespace std;
int t,n,k;
int main()
{
cin >> t;
while(t--){
cin >> n >> k;
string s; cin >> s;
sort(s.begin(),s.end());
if(s[0] != s[k-1]){
cout << s[k-1] <<endl;
continue;
}
cout << s[0] ;
if(s[k] == s[n-1]){
for(int i = k;i < n;i += k) cout << s[i];
}else{
for(int i = k;i < n;i++){
cout << s[i];
}
}
cout << endl;
}
return 0;
}