CF1316B.String Modification

普及/提高-

通过率:0%

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 , ( 1kn1 \leq k \leq n ).
  2. For ii from 11 to nk+1n-k+1 , reverse the substring s[i:i+k1]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 aba \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 .

输入格式

Each test contains multiple test cases.

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

The first line of each test case contains a single integer nn ( 1n50001 \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 .

输出格式

For each testcase output two lines:

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

In the second line output the appropriate value of kk ( 1kn1 \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.

输入输出样例

  • 输入#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 : babaThe lexicographically smallest string achievable through modification is abab for k=1k = 1 and 33 . Smallest value of kk needed to achieve is hence 11 .
首页