311371: CF1975H. 378QAQ and Core
Description
378QAQ has a string $s$ of length $n$. Define the core of a string as the substring$^\dagger$ with maximum lexicographic$^\ddagger$ order.
For example, the core of "$\mathtt{bazoka}$" is "$\mathtt{zoka}$", and the core of "$\mathtt{aaa}$" is "$\mathtt{aaa}$".
378QAQ wants to rearrange the string $s$ so that the core is lexicographically minimum. Find the lexicographically minimum possible core over all rearrangements of $s$.
$^\dagger$ A substring of string $s$ is a continuous segment of letters from $s$. For example, "$\mathtt{defor}$", "$\mathtt{code}$" and "$\mathtt{o}$" are all substrings of "$\mathtt{codeforces}$" while "$\mathtt{codes}$" and "$\mathtt{aaa}$" are not.
$^\ddagger$ A string $p$ is lexicographically smaller than a string $q$ if and only if one of the following holds:
- $p$ is a prefix of $q$, but $p \ne q$; or
- in the first position where $p$ and $q$ differ, the string $p$ has a smaller element than the corresponding element in $q$ (when compared by their ASCII code).
For example, "$\mathtt{code}$" and "$\mathtt{coda}$" are both lexicographically smaller than "$\mathtt{codeforces}$" while "$\mathtt{codeforceston}$" and "$\mathtt{z}$" are not.
InputEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 10^5$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1\leq n\leq 10^6$) — the length of string $s$.
The next line of each test case contains the string $s$ of length $n$. The string $s$ consists of lowercase English letters.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
OutputFor each test case, output the lexicographically minimum possible core over all rearrangements of $s$.
ExampleInput6 3 qaq 4 cccc 6 bazoka 6 zazzzz 7 ababbbb 7 ccbabccOutput
qaq cccc z zzz bbababb cbcacbcNote
In the first test case, all possible rearrangements and their corresponding cores are as follows:
- "$\mathtt{qaq}$", its core is "$\mathtt{qaq}$".
- "$\mathtt{aqq}$", its core is "$\mathtt{qq}$".
- "$\mathtt{qqa}$", its core is "$\mathtt{qqa}$".
So the core with the minimum lexicographic order in all rearrangement plans is "$\mathtt{qaq}$".
Output
378QAQ有一个长度为n的字符串s。定义一个字符串的“核心”为具有最大字典序的子串。例如,字符串"bazoka"的核心是"zoka",而字符串"aaa"的核心是"aaa"。378QAQ想要重新排列字符串s,使得核心在字典序上最小。找出所有s的排列中字典序最小的可能核心。
输入数据格式:
每个测试包含多个测试用例。第一行包含测试用例的数量t(1≤t≤10^5)。接下来是每个测试用例的描述。
每个测试用例的第一行包含一个整数n(1≤n≤10^6)——字符串s的长度。
下一行包含长度为n的字符串s。字符串s由小写英文字母组成。
保证所有测试用例的n之和不超过10^6。
输出数据格式:
对于每个测试用例,输出所有s的排列中字典序最小的可能核心。
例:
输入
```
6
3
qaq
4
cccc
6
bazoka
6
zazzzz
7
ababbbb
7
ccbabcc
```
输出
```
qaq
cccc
z
zzz
bbababb
cbcacbc
```
注意:
在第一个测试用例中,所有可能的排列及其对应的核心如下:
- "qaq",其核心是"qaq"。
- "aqq",其核心是"qq"。
- "qqa",其核心是"qqa"。
所以,在所有排列计划中,字典序最小的核心是"qaq"。题目大意: 378QAQ有一个长度为n的字符串s。定义一个字符串的“核心”为具有最大字典序的子串。例如,字符串"bazoka"的核心是"zoka",而字符串"aaa"的核心是"aaa"。378QAQ想要重新排列字符串s,使得核心在字典序上最小。找出所有s的排列中字典序最小的可能核心。 输入数据格式: 每个测试包含多个测试用例。第一行包含测试用例的数量t(1≤t≤10^5)。接下来是每个测试用例的描述。 每个测试用例的第一行包含一个整数n(1≤n≤10^6)——字符串s的长度。 下一行包含长度为n的字符串s。字符串s由小写英文字母组成。 保证所有测试用例的n之和不超过10^6。 输出数据格式: 对于每个测试用例,输出所有s的排列中字典序最小的可能核心。 例: 输入 ``` 6 3 qaq 4 cccc 6 bazoka 6 zazzzz 7 ababbbb 7 ccbabcc ``` 输出 ``` qaq cccc z zzz bbababb cbcacbc ``` 注意: 在第一个测试用例中,所有可能的排列及其对应的核心如下: - "qaq",其核心是"qaq"。 - "aqq",其核心是"qq"。 - "qqa",其核心是"qqa"。 所以,在所有排列计划中,字典序最小的核心是"qaq"。