Codeforces Round 947 (Div. 1 + Div. 2)
H
378QAQ and Core
Codeforces
3500
原题
← GI →
翻译中…

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.

翻译中…

Input

翻译中…

Each 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$.

翻译中…

Output

翻译中…

For each test case, output the lexicographically minimum possible core over all rearrangements of $s$.

翻译中…

Note

翻译中…

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}$".

翻译中…

样例

Input #1
6
3
qaq
4
cccc
6
bazoka
6
zazzzz
7
ababbbb
7
ccbabcc
Output #1
qaq
cccc
z
zzz
bbababb
cbcacbc
InputCopy OutputCopy
题目信息
题号
1975H
时间限制
2 s
内存限制
256 MB
标签
贪心
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。