C. Phoenix and Distribution

Phoenix has a string ss consisting of lowercase Latin letters. He wants to distribute all the letters of his string into knon-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 xyx≠y, or there exists an index ii (1imin(|x|,|y|))1≤i≤min(|x|,|y|)) such that xixi < yiyi and for every j(1j<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 (1t10001≤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 (1kn1051≤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
Copy
6
4 2
baba
5 2
baacb
5 3
baacb
5 3
aaaaa
6 4
aaxxzz
7 1
phoenix
output
Copy
ab
abbc
b
aa
x
ehinopx
Note

In the first test case, one optimal solution is to distribute baba into ab and ab.

In the second test case, one optimal solution is to distribute baacb into abbc and a.

In the third test case, one optimal solution is to distribute baacb into ac, ab, and b.

In the fourth test case, one optimal solution is to distribute aaaaa into aa, aa, and a.

In the fifth test case, one optimal solution is to distribute aaxxzz into az, az, x, and x.

In the sixth test case, one optimal solution is to distribute phoenix into ehinopx.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 998244353;
const int N = 1e6+5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n, k;
        cin >> n >> k;
        string s,res="";
        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++)
                cout << s[i];
            cout << endl;
        }
        else
        {
            double n1=n, k1=k;
            cout << s.substr(k, ceil((n1-k1)/ k1)) << endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dealer/p/12837448.html