String Modification
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
String Modification
题面翻译
题目描述
Vasya 有一个长度为 的字符串 。他想要对它进行以下的修改:
- 选择一个整数 且 。
- 让 从 循环到 ,每一次反转 在 范围中的子串。
比如说,字符串 是 且 ,以下就是 被修改的过程:
- (原字符串)
- (旋转了第一个长为 的子串)
- (旋转了第二个长为 的子串)
- (旋转了最后一个长为 的子串)
所以, 经过 的一系列变化后最终会变成 。
Vasya 希望选择一个 ,使得经过上述变化的字符串字典序最小。如果多个不同的 都能满足要求,他想要取最小的一个。他正忙着参加 Felicity 2020,于是他叫你来帮他。
一个字符串 比 字典序更小需要以下条件中只有一个满足:
- 是 的前缀,但 ;
- 在从左往右 和 第一个不同的位置, 上的字符在字母表中比 上字符更靠前。
输入格式
每一个测试点有多组数据。
第一行有一个整数 表示数据组数。
每组数据第一行包含一个整数 ,表示字符串长度。
第二行包含一个有 个小写英文字母的字符串 。
保证每组数据的 不会超过 。
输出格式
对于每一组数据打印两行:
第一行是可以达到的字典序最小的字符串 。
第二行是与之相应的 用来进行修改。如果多个 都可以达到字典序最小的字符串,请打印最小的一个 。
题目描述
Vasya has a string of length . He decides to make the following modification to the string:
- Pick an integer , ( ).
- For from to , reverse the substring of . For example, if string is qwer and , below is the series of transformations the string goes through:
- qwer (original string)
- wqer (after reversing the first substring of length )
- weqr (after reversing the second substring of length )
- werq (after reversing the last substring of length )
Hence, the resulting string after modifying with is werq.
Vasya wants to choose a such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of . Among all such , he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.
A string is lexicographically smaller than a string if and only if one of the following holds:
- is a prefix of , but ;
- in the first position where and differ, the string has a letter that appears earlier in the alphabet than the corresponding letter in .
输入格式
Each test contains multiple test cases.
The first line contains the number of test cases ( ). The description of the test cases follows.
The first line of each test case contains a single integer ( ) — the length of the string .
The second line of each test case contains the string of lowercase latin letters.
It is guaranteed that the sum of over all test cases does not exceed .
输出格式
For each testcase output two lines:
In the first line output the lexicographically smallest string achievable after the above-mentioned modification.
In the second line output the appropriate value of ( ) that you chose for performing the modification. If there are multiple values of that give the lexicographically smallest string, output the smallest value of among them.
样例 #1
样例输入 #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 : abab
- for : baba
- for : abab
- for : babaThe lexicographically smallest string achievable through modification is abab for and . Smallest value of needed to achieve is hence .