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
inputCopy
6
4
abab
6
qwerty
5
aaaaa
6
alaska
9
lfpbavjsm
1
p
outputCopy
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最小

思路:比赛时候被卡了觉得大暴力会T 所以就找规律了
然后就发现规律了。。。
选择k的时候 构造的字符串首先是从a_k到a_len 然后后面拼接就是前面的一部分,如果k和len的奇偶性一样 就倒着拼接,否则就顺着

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define paii pair<int,int>
#define pali pair<ll,int>
#define pail pair<int,ll>
#define pall pair<ll,ll>
#define fi first
#define se second
string s;
struct node
{
    string c;
    int k;
} a[5005];
bool cmp(node a, node b)
{
    if(a.c==b.c)
        return a.k<b.k;
    return a.c<b.c;
}
int main()
{
    int t;
    while(cin>>t)
    {
        while(t--)
        {
            int len;
            cin>>len>>s;
            a[0].c=s,a[0].k=1;

            for(int i=1; i<len; i++)
            {
                string q="";
                for(int j=i; j<len; j++)
                {
                    q+=s[j];
                }

                if((i+1)%2==0 && len%2==0  || (i+1)%2==1 && len%2==1)
                    for(int j=i-1; j>=0; j--)
                        q+=s[j];
                else
                    for(int j=0; j<i; j++)
                        q+=s[j];

                a[i].c=q,a[i].k=i+1;
            }

            sort(a,a+len,cmp);
            //for(int i=0;i<len;i++)
            cout<<a[0].c<<endl<<a[0].k<<endl;
        }
    }
    return 0;
}