CF1530E.Minimax

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Prefix function of string t=t1t2…tnt = t_1 t_2 \ldots t_n and position ii in it is defined as the length kk of the longest proper (not equal to the whole substring) prefix of substring t1t2…tit_1 t_2 \ldots t_i 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,…,71, 2, \ldots, 7 are equal to [0,0,1,0,1,2,3][0, 0, 1, 0, 1, 2, 3].

Let f(t)f(t) be equal to the maximum value of the prefix function of string tt over all its positions. For example, f(f(abacaba)=3) = 3.

You are given a string ss. Reorder its characters arbitrarily to get a string tt (the number of occurrences of any character in strings ss and tt must be equal). The value of f(t)f(t) must be minimized. Out of all options to minimize f(t)f(t), choose the one where string tt is the lexicographically smallest.

字符串 t=t1t2…tnt = t_1 t_2 \ldots t_n 在位置 ii 处的前缀函数定义为:子串 t1t2…tit_1 t_2 \ldots t_i 的最长真前缀(即不等于该子串自身的前缀)的长度 kk,且该前缀同时也是该子串的一个后缀。

例如,对字符串 $t = $ abacaba,其在位置 1,2,…,71, 2, \ldots, 7 处的前缀函数值分别为 [0,0,1,0,1,2,3][0, 0, 1, 0, 1, 2, 3]。

令 f(t)f(t) 表示字符串 tt 在所有位置上的前缀函数值的最大值。例如,f(f(abacaba)=3) = 3。

现给定一个字符串 ss。你可以任意重排其字符,得到字符串 tt(即 ss 与 tt 中每个字符的出现次数必须完全相同)。要求使 f(t)f(t) 尽可能小;在所有能使 f(t)f(t) 取得最小值的方案中,选择字典序最小的字符串 tt。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051 \le t \le 10^5). Description of the test cases follows.

The only line of each test case contains string ss (1≤∣s∣≤1051 \le |s| \le 10^5) consisting of lowercase English letters.

It is guaranteed that the sum of lengths of ss over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1051 \le t \le 10^5)。随后是测试用例的描述。

每个测试用例仅有一行,包含字符串 ss(1≤∣s∣≤1051 \le |s| \le 10^5),该字符串由小写英文字母组成。

保证所有测试用例中字符串 ss 的长度总和不超过 10510^5。

输出格式

For each test case print a single string tt.

The multisets of letters in strings ss and tt must be equal. The value of f(t)f(t), the maximum of prefix functions in string tt, must be as small as possible. String tt must be the lexicographically smallest string out of all strings satisfying the previous conditions.

对每个测试用例,输出一个字符串 tt。

字符串 ss 和 tt 中字母的多重集合必须相等。tt 的函数值 f(t)f(t)(即字符串 tt 的所有前缀函数值中的最大值)必须尽可能小。在满足前述条件的所有字符串中,tt 必须是字典序最小的字符串。

输入输出样例

  • 输入#1

    3
    vkcup
    abababa
    zzzzzz

    输出#1

    ckpuv
    aababab
    zzzzzz

说明/提示

A string aa is lexicographically smaller than a string bb if and only if one of the following holds:

  • aa is a prefix of bb, but a≠ba \ne b;
  • in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.

In the first test case, f(t)=0f(t) = 0 and the values of prefix function are [0,0,0,0,0][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)=1f(t) = 1 and the values of prefix function are [0,1,0,1,0,1,0][0, 1, 0, 1, 0, 1, 0].

In the third test case, f(t)=5f(t) = 5 and the values of prefix function are [0,1,2,3,4,5][0, 1, 2, 3, 4, 5].

字符串 aa 在字典序上小于字符串 bb,当且仅当以下条件之一成立:

  • aa 是 bb 的前缀,但 a≠ba \ne b;
  • 在 aa 与 bb 首次出现差异的位置上,aa 中的字母在字母表中位于 bb 中对应字母之前。

在第一个测试用例中,f(t)=0f(t) = 0,且对于字母的任意排列,其前缀函数值均为 [0,0,0,0,0][0, 0, 0, 0, 0]。字符串 ckpuv 是字符串 vkcup 的字母所构成的所有排列中字典序最小的一个。

在第二个测试用例中,f(t)=1f(t) = 1,其前缀函数值为 [0,1,0,1,0,1,0][0, 1, 0, 1, 0, 1, 0]。

在第三个测试用例中,f(t)=5f(t) = 5,其前缀函数值为 [0,1,2,3,4,5][0, 1, 2, 3, 4, 5]。

输入解题思路,AI测评打分。不知道怎么写?

首页