Codeforce Div2 ——B. String Modification

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

Pick an integer kk, (1≤k≤n1≤k≤n).
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≠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.

Input
Each test contains multiple test cases
The first line contains the number of test cases tt (1≤t≤50001≤t≤5000). The description of the test cases follows
The first line of each test case contains a single integer nn (1≤n≤50001≤n≤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.

output
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≤k≤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.

example input

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

example 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=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.

经过研究样例可以发现规律,每组的输出都是截取前面字符串的一部分进行翻转后放到另一部分字符串的后面,并且当另一部分字符串字符个数是偶数时截取的那一部分进行翻转,奇数时不翻转,这样,我们枚举所有的情况进行比较,会得到最合适的答案

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
// *start on @date: 2020-02-29 14:21 
using namespace std;
int main()
{
    int m;
    cin>>m;
    while(m--)
    {
        int n;
        int flag=1;
        string s ;
        cin >> n;
        cin >> s;
        string k ="zzzzzz";
        for(int i=0; i<n; i++)
        {
            string a = s.substr(i);
            string b = s.substr(0,i);
            if((n-i)%2)
            {
                reverse(b.begin(),b.end());
                a+=b;
            }
            else a+=b;
            if(k>a)
            {
                flag=i+1;
                k = a;
            }
        }
        cout<<k<<endl<<flag<<endl;
    }      
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值