CF1781C.Equal Frequencies

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Let's call a string balanced if all characters that are present in it appear the same number of times. For example, "coder", "appall", and "ttttttt" are balanced, while "wowwow" and "codeforces" are not.

You are given a string ss of length nn consisting of lowercase English letters. Find a balanced string tt of the same length nn consisting of lowercase English letters that is different from the string ss in as few positions as possible. In other words, the number of indices ii such that si≠tis_i \ne t_i should be as small as possible.

我们称一个字符串是平衡的,当且仅当其中所有出现的字符的出现次数均相同。例如,“coder”、“appall”和“ttttttt”是平衡的,而“wowwow”和“codeforces”则不是。

给定一个长度为 nn 的字符串 ss,它仅由小写英文字母组成。请找出一个长度同样为 nn、也仅由小写英文字母组成的平衡字符串 tt,使得 tt 与 ss 在尽可能少的位置上不同。换言之,使得满足 si≠tis_i \ne t_i 的下标 ii 的个数最小。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

Each test case consists of two lines. The first line contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the length of the string ss.

The second line contains the string ss of length nn consisting of lowercase English letters.

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

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

每个测试用例由两行组成。第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)—— 字符串 ss 的长度。

第二行包含一个长度为 nn 的字符串 ss,由小写英文字母组成。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

For each test case, print the smallest number of positions where string ss and a balanced string tt can differ, followed by such a string tt.

If there are multiple solutions, print any. It can be shown that at least one balanced string always exists.

对于每个测试用例,输出字符串 ss 与一个平衡字符串 tt 之间可能的最小差异位置数,并随后输出这样一个字符串 tt。

若存在多个解,输出任意一个即可。可以证明,至少存在一个平衡字符串。

输入输出样例

  • 输入#1

    4
    5
    hello
    10
    codeforces
    5
    eevee
    6
    appall

    输出#1

    1
    helno
    2
    codefofced
    1
    eeeee
    0
    appall

说明/提示

In the first test case, the given string "hello" is not balanced: letters 'h', 'e', and 'o' appear in it once, while letter 'l' appears twice. On the other hand, string "helno" is balanced: five distinct letters are present in it, and each of them appears exactly once. Strings "hello" and "helno" differ in just one position: the fourth character. Other solutions are possible too.

In the second test case, string "codefofced" is balanced since only letters 'c', 'o', 'd', 'e', and 'f' are present in it, and each of them appears exactly twice.

In the third test case, string "eeeee" is balanced since only letter 'e' is present in it.

In the fourth test case, the given string "appall" is already balanced.

在第一个测试用例中,给定字符串 "hello" 不是平衡的:其中字母 'h'、'e' 和 'o' 各出现一次,而字母 'l' 出现了两次。另一方面,字符串 "helno" 是平衡的:它包含恰好五个互不相同的字母,且每个字母均恰好出现一次。字符串 "hello" 和 "helno" 仅在第四个位置上不同。其他可行解也存在。

在第二个测试用例中,字符串 "codefofced" 是平衡的,因为其中仅包含字母 'c'、'o'、'd'、'e' 和 'f',且每个字母均恰好出现两次。

在第三个测试用例中,字符串 "eeeee" 是平衡的,因为其中仅包含字母 'e'。

在第四个测试用例中,给定字符串 "appall" 已经是平衡的。

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

首页