codeforces B. String Modification

B. String Modification

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya has a string s of length n. He decides to make the following modification to the string:

Pick an integer k, (1≤k≤n).
For i from 1 to n−k+1, reverse the substring s[i:i+k−1] of s. For example, if string s is qwer and k=2, below is the series of transformations the string goes through:
qwer (original string)
wqer (after reversing the first substring of length 2)
weqr (after reversing the second substring of length 2)
werq (after reversing the last substring of length 2)
Hence, the resulting string after modifying s with k=2 is werq.
Vasya wants to choose a k such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of k. Among all such k, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.

A string a is lexicographically smaller than a string b if and only if one of the following holds:

a is a prefix of b, but a≠b;
in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.

Input

Each test contains multiple test cases.

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

The first line of each test case contains a single integer n (1≤n≤5000) — the length of the string s.

The second line of each test case contains the string s of n lowercase latin letters.

It is guaranteed that the sum of n over all test cases does not exceed 5000.

Output

For each testcase output two lines:

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

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

Example

input

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

output

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

Note

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

for k=1 : abab
for k=2 : baba
for k=3 : abab
for k=4 : baba
The lexicographically smallest string achievable through modification is abab for k=1 and 3. Smallest value of k needed to achieve is hence 1.

题意:

给你一个字符串,求出每k个字符进行反转后,字符串最小的情况下,k最小的情况,最后输出k和反转后的字符串。

思路:

这题首先应该想的是如何求出反转后的k,如果真的用reverse进行暴力反转的话,可能会超时,但是我们稍微模拟一下,比如第二个例子,qwerty模拟的话,就会有
ewqrty->erqwty->ertwqy->ertyqw;
这个时候我们会发现加入是i为中心发生反转的时候,swap(a[i - 1], a[i + 1]),之后i+1之后swap(a[i], a[i + 2])所以从头开始就是a[i + 1]+a[i + 2]+……,也就是顺序输出,也就是k==3的时候从e开始顺序输出,这个时候看前面的怎么输出,这个时候就和后缀有关系了,后缀如果是奇数(即n-k为奇数),那么就要反转n-k+1次,即反转偶数次,那么前缀相当于没有反转,偶数反之,最后暴力一下就出来了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
typedef long long ll;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
int main() {
    int t, n;
    read(t);
    while (t--) {
        read(n);
        string s;
        cin >> s;
        string t = s;
        int x = 1;
        for (int k = 2; k <= s.size(); k++) {
            string tmp = "";
            for (int i = k - 1; i < s.size(); i++) {
                tmp += s[i];
            }
            if ((s.size() - k) % 2 == 0) {
                for (int i = k - 2; i >= 0; i--) {
                    tmp += s[i];
                }
            } else {
                for (int i = 0; i < k - 1; i++) {
                    tmp += s[i];
                }
            }
            if (tmp < t) {
                t = tmp;
                x = k;
            }
        }
        cout << t << endl;
        out(x);
        putchar('\n');
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值