CF1493C.K-beautiful Strings

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a string ss consisting of lowercase English letters and a number kk. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by kk. You are asked to find the lexicographically smallest beautiful string of length nn, which is lexicographically greater or equal to string ss. If such a string does not exist, output −1-1.

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.

给你一个由小写英文字母组成的字符串 ss 和一个整数 kk。我们称一个仅由小写英文字母组成的字符串为“优美的”,当且仅当其中每个字母的出现次数均能被 kk 整除。你需要找出长度为 nn 的字典序最小的优美字符串,且该字符串的字典序大于或等于字符串 ss。如果不存在这样的字符串,则输出 −1-1。

字符串 aa 的字典序小于字符串 bb,当且仅当满足以下条件之一:

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

输入格式

The first line contains a single integer TT (1≤T≤10 0001 \le T \le 10\,000) — the number of test cases.

The next 2⋅T2 \cdot T lines contain the description of test cases. The description of each test case consists of two lines.

The first line of the description contains two integers nn and kk (1≤k≤n≤1051 \le k \le n \le 10^5) — the length of string ss and number kk respectively.

The second line contains string ss 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≤10 0001 \le T \le 10\,000),表示测试用例的数量。

接下来的 2⋅T2 \cdot T 行描述了各个测试用例。每个测试用例的描述由两行组成。

描述的第一行包含两个整数 nn 和 kk(1≤k≤n≤1051 \le k \le n \le 10^5),分别表示字符串 ss 的长度和数字 kk。

描述的第二行包含一个由小写英文字母组成的字符串 ss。

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

输出格式

For each test case output in a separate line lexicographically smallest beautiful string of length nn, which is greater or equal to string ss, or −1-1 if such a string does not exist.

对于每个测试用例,在单独一行中输出长度为 nn 的字典序最小的优美字符串,该字符串大于或等于字符串 ss;如果不存在这样的字符串,则输出 −1-1。

输入输出样例

  • 输入#1

    4
    4 2
    abcd
    3 1
    abc
    4 3
    aaaa
    9 3
    abaabaaaa

    输出#1

    acac
    abc
    -1
    abaabaaab

说明/提示

In the first test case "acac" is greater than or equal to ss, and each letter appears 22 or 00 times in it, so it is beautiful.

In the second test case each letter appears 00 or 11 times in ss, so ss itself is the answer.

We can show that there is no suitable string in the third test case.

In the fourth test case each letter appears 00, 33, or 66 times in "abaabaaab". All these integers are divisible by 33.

在第一个测试用例中,“acac”大于或等于 ss,且其中每个字母出现 22 次或 00 次,因此它是优美的。

在第二个测试用例中,ss 中每个字母出现 00 次或 11 次,因此 ss 本身即为答案。

我们可以证明:第三个测试用例中不存在满足条件的字符串。

在第四个测试用例中,“abaabaaab”中每个字母出现的次数为 00、33 或 66。所有这些整数均可被 33 整除。

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

首页