CF1316B.String Modification

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Vasya has a string ss of length nn. He decides to make the following modification to the string:

  1. Pick an integer kk, (1≤k≤n1 \leq k \leq n).

  2. For ii from 11 to n−k+1n-k+1, reverse the substring s[i:i+k−1]s[i:i+k-1] of ss. For example, if string ss is qwer and k=2k = 2, below is the series of transformations the string goes through:

    • qwer (original string)
    • wqer (after reversing the first substring of length 22)
    • weqr (after reversing the second substring of length 22)
    • werq (after reversing the last substring of length 22)

    Hence, the resulting string after modifying ss with k=2k = 2 is werq.

Vasya wants to choose a kk such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of kk. Among all such kk, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.

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.

Vasya 有一个长度为 nn 的字符串 ss。他决定对这个字符串进行如下修改:

  1. 选择一个整数 kk(满足 1≤k≤n1 \leq k \leq n);

  2. 对于每个 ii 从 11 到 n−k+1n-k+1,将 ss 的子串 s[i:i+k−1]s[i:i+k-1] 进行翻转。例如,若字符串 ss 为 qwer 且 k=2k = 2,则字符串经历以下一系列变换:

    • qwer(原始字符串)
    • wqer(翻转第一个长度为 22 的子串后)
    • weqr(翻转第二个长度为 22 的子串后)
    • werq(翻转最后一个长度为 22 的子串后)

    因此,对 ss 使用 k=2k = 2 修改后得到的最终字符串为 werq。

Vasya 希望选出一个 kk,使得经过上述修改后所得的字符串在所有可能的 kk 中字典序最小。在所有能达成该最小字典序的 kk 中,他希望选出最小的那个。由于他正忙于参加 Felicity 2020,因此请求你的帮助。

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

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

输入格式

Each test contains multiple test cases.

The first line contains the number of test cases tt (1≤t≤50001 \le t \le 5000). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤50001 \le n \le 5000) — the length of the string ss.

The second line of each test case contains the string ss of nn lowercase latin letters.

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

每个测试包含多个测试用例。

第一行包含测试用例的数量 tt(1≤t≤50001 \le t \le 5000)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤50001 \le n \le 5000)—— 字符串 ss 的长度。

每个测试用例的第二行包含由 nn 个小写拉丁字母组成的字符串 ss。

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

输出格式

For each testcase output two lines:

In the first line output the lexicographically smallest string s′s' achievable after the above-mentioned modification.

In the second line output the appropriate value of kk (1≤k≤n1 \leq k \leq n) that you chose for performing the modification. If there are multiple values of kk that give the lexicographically smallest string, output the smallest value of kk among them.

对于每个测试用例,输出两行:

第一行输出经过上述修改后能得到的字典序最小的字符串 s′s'。

第二行输出你所选择的用于执行该修改的 kk 值(1≤k≤n1 \leq k \leq n)。如果存在多个 kk 值能产生字典序最小的字符串,则输出其中最小的 kk 值。

输入输出样例

  • 输入#1

    6
    4
    abab
    6
    qwerty
    5
    aaaaa
    6
    alaska
    9
    lfpbavjsm
    1
    p

    输出#1

    abab
    1
    ertyqw
    3
    aaaaa
    1
    aksala
    6
    avjsmbpfl
    5
    p
    1

说明/提示

In the first testcase of the first sample, the string modification results for the sample abab are as follows :

  • for k=1k = 1 : abab

  • for k=2k = 2 : baba

  • for k=3k = 3 : abab

  • for k=4k = 4 : baba

    The lexicographically smallest string achievable through modification is abab for k=1k = 1 and 33. Smallest value of kk needed to achieve is hence 11.

在第一个样例的第一个测试用例中,字符串 abab 经过修改后得到的结果如下:

  • 当 k=1k = 1 时:abab
  • 当 k=2k = 2 时:baba
  • 当 k=3k = 3 时:abab
  • 当 k=4k = 4 时:baba

通过修改所能得到的字典序最小的字符串是 abab(对应 k=1k = 1 和 k=3k = 3)。因此,达到该最小字典序字符串所需的最小 kk 值为 11。

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

首页