#638 (Div. 2)C. Phoenix and Distribution(分类讨论)

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 a1,a2,…,ak such that every letter of s goes to exactly one of the strings ai. The strings ai do not need to be substrings of s. Phoenix can distribute letters of s and rearrange the letters within each string ai however he wants.
For example, if s= baba and k=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 (ai should be non-empty)
Phoenix wants to distribute the letters of his string s into k strings a1,a2,…,ak to minimize the lexicographically maximum string among them, i. e. minimize max(a1,a2,…,ak). Help him find the optimal distribution and print the minimal possible value of max(a1,a2,…,ak).
String x is lexicographically less than string y if either x is a prefix of y and x≠y, or there exists an index i (1≤i≤min(|x|,|y|)) such that xi < yi and for every j (1≤j<i) xj=yj. Here |x| denotes the length of the string x.

Input

The input consists of multiple test cases. The first line contains an integer t (1≤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 n and k (1≤k≤n≤105) — the length of string s and the number of non-empty strings, into which Phoenix wants to distribute letters of s, respectively.
The second line of each test case contains a string s of length n consisting only of lowercase Latin letters.
It is guaranteed that the sum of n over all test cases is ≤105.

Output

Print t answers — one per test case. The i-th answer should be the minimal possible value of max(a1,a2,…,ak) in the i-th test case.

Example

inputCopy
6
4 2
baba
5 2
baacb
5 3
baacb
5 3
aaaaa
6 4
aaxxzz
7 1
phoenix
outputCopy
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.

题目大意:
给你n个字符,要求你将其分为k份,每份个数至少有1个,使得新组合的字符串中字典序最大的字符串的字典序尽可能小,打印这个字符串。

题目分析:
首先给s进行排序,在将s中前k个字符分配到a[1]-a[k]这k个字符串中。
这时进行第一个分类讨论:

  1. s中最小的字母有至少k个(s[0]==s[k-1]),则继续讨论。
  2. s中最小的字母小于k个(s[0]==s[k-1]),此时答案为s[k-1]。

然后判断后面k+1到n个字母是否相同

  1. 后面k到n个字母不相同(s[k]!=s[n-1]) ,那么答案即为s[0],s[k]-s[n-1] (这样才能保证最大字典序最小)。
  2. 后面k到n个字母相同 (s[k]==s[n-1]),那么答案即为将s均分后长度最长的字符串。
    例如:
    9 4
    aaaabbbbb
    将其均分后四个组分别为:ab,ab,ab,abb
    答案即为abb。

代码如下:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=1e5+5;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		string s;
		int n,k;
		cin>>n>>k;
		cin>>s;
		sort(s.begin(),s.end());     //排序
		if(s[0]!=s[k-1]) {cout<<s[k-1]<<endl; continue;}   //第一个分类讨论
		
		cout<<s[0];        //后面两种情况都包含s[0],因此先输出
		if(s[k]==s[n-1])   //第二个分类讨论
		{   //第二种情况
		    for(int i=0;i<(n-1)/k;i++) 
			cout<<s[n-1];
		} 
		else
		{   //第一种情况
			for(int i=k;i<n;i++)
			cout<<s[i];
		}
		cout<<endl; 
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/li_wen_zhuo/article/details/105886852