CF1530E.Minimax
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Prefix function of string t=t1t2…tn and position i in it is defined as the length k of the longest proper (not equal to the whole substring) prefix of substring t1t2…ti which is also a suffix of the same substring.
For example, for string $t = $ abacaba the values of the prefix function in positions 1,2,…,7 are equal to [0,0,1,0,1,2,3] .
Let f(t) be equal to the maximum value of the prefix function of string t over all its positions. For example, f( abacaba )=3 .
You are given a string s . Reorder its characters arbitrarily to get a string t (the number of occurrences of any character in strings s and t must be equal). The value of f(t) must be minimized. Out of all options to minimize f(t) , choose the one where string t is the lexicographically smallest.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤105 ). Description of the test cases follows.
The only line of each test case contains string s ( 1≤∣s∣≤105 ) consisting of lowercase English letters.
It is guaranteed that the sum of lengths of s over all test cases does not exceed 105 .
输出格式
For each test case print a single string t .
The multisets of letters in strings s and t must be equal. The value of f(t) , the maximum of prefix functions in string t , must be as small as possible. String t must be the lexicographically smallest string out of all strings satisfying the previous conditions.
输入输出样例
输入#1
3 vkcup abababa zzzzzz
输出#1
ckpuv aababab zzzzzz
说明/提示
A string a is lexicographically smaller than a string b if and only if one of the following holds:
- a is a prefix of b , but a=b ;
- in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b .
In the first test case, f(t)=0 and the values of prefix function are [0,0,0,0,0] for any permutation of letters. String ckpuv is the lexicographically smallest permutation of letters of string vkcup.
In the second test case, f(t)=1 and the values of prefix function are [0,1,0,1,0,1,0] .
In the third test case, f(t)=5 and the values of prefix function are [0,1,2,3,4,5] .