Minimax solution codeforces
Prefix function of string and position in it is defined as the length of the longest proper (not equal to the whole substring) prefix of substring which is also a suffix of the same substring.
For example, for string abacaba the values of the prefix function in positions are equal to .
Let maximum value of the prefix function of string over all its positions. For example, abacaba.be equal to the
You are given a string minimized. Out of all options to minimize , choose the one where string is the lexicographically smallest.. Reorder its characters arbitrarily to get a string (the number of occurrences of any character in strings and must be equal). The value of must be
Each test contains multiple test cases. The first line contains the number of test cases( ). Description of the test cases follows.
The only line of each test case contains string( ) consisting of lowercase English letters.
It is guaranteed that the sum of lengths ofover all test cases does not exceed .
For each test case print a single string.
The multisets of letters in stringsand must be equal. The value of , the maximum of prefix functions in string , must be as small as possible. String must be the lexicographically smallest string out of all strings satisfying the previous conditions.
3 vkcup abababa zzzzzz
ckpuv aababab zzzzzz
A stringis lexicographically smaller than a string if and only if one of the following holds:
- is a prefix of , but ;
- in the first position where and differ, the string has a letter that appears earlier in the alphabet than the corresponding letter in .
In the first test case, ckpuv is the lexicographically smallest permutation of letters of string vkcup.and the values of prefix function are for any permutation of letters. String
In the second test case,and the values of prefix function are .
In the third test case,and the values of prefix function are .