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.

//规律从后往前数,分奇偶,然后要么是直接放后面,要么是导致,直接写string模拟这过程完事了。

#include <bits/stdc++.h>
using namespace std;
template <typename t>
void read(t &x)
{
char ch = getchar();
x = 0;
t f = 1;
while (ch < '0' || ch > '9')
f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
x *= f;
} #define wi(n) printf("%d ", n)
#define wl(n) printf("%lld ", n)
#define rep(m, n, i) for (int i = m; i < n; ++i)
#define rrep(m, n, i) for (int i = m; i > n; --i)
#define P puts(" ")
typedef long long ll;
#define MOD 1000000007
#define mp(a, b) make_pair(a, b)
#define N 200005
#define fil(a, n) rep(0, n, i) read(a[i])
//---------------https://lunatic.blog.csdn.net/-------------------//
int n;
string c; int main()
{
int t, f;
read(t);
while (t--)
{
read(n);
cin >> c;
f=0;
string ans = c, s;
for (int i = 1; i < n; i++)
{
if ((n - i) % 2 == 0)
s = c.substr(i, n - i) + c.substr(0, i );
else
{
string tem = c.substr(0, i );
reverse(tem.begin(), tem.end());
s = c.substr(i, n - i) +tem;
}
if (ans > s)
{
f = i;
ans = s;
}
}
cout << ans << endl; wi(f + 1), P;
}
}

Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)的更多相关文章

  1. CodeCraft-20 (Div. 2) B. String Modification (字符串,规律)

    题意:有一个长度为\(n\)的字符串,你可以选取一个值\(k(1\le k \le n)\),然后遍历字符串,每次将区间长度为\(k\)的字符串反转,求反转后字典序最小的字符串,并输出\(k\)的值. ...

  2. Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律

    C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...

  3. Div 3 - SGU 105(找规律)

    分析:很容易知道序列1,2,3, 4,5, 6......与3的关系就是1,2, 0,1, 2,0,......如果是在一个数后面添加一个数就变成了这种序列1, 0, 0, 1, 0, 0, 1, 0 ...

  4. Codeforces Round #347 (Div. 2) C. International Olympiad 找规律

    题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...

  5. BestCoder Round #81 (div.2) 1004 String(动态规划)

    题目链接:BestCoder Round #81 (div.2) 1003 String 题意 中文题,上有链接.就不贴了. 思路 枚举起点i,计算能够达到k个不同字母的最小下标j,则此时有子串len ...

  6. js比较两个String字符串找出不同,并将不同处高亮显示

    根据java代码改写成js,下边js文件代码: function StringBuffer() { this.__strings__ = []; }; StringBuffer.prototype.a ...

  7. 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake

    题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...

  8. Codeforce 424C Magic Formulas 找规律

    题目链接:http://codeforces.com/contest/424/problem/C 题意:求Q值 思路:找规律 显然能够得到一个矩阵 把这个矩阵画出来就能发现一个横向的规律和一个主对角线 ...

  9. Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)

    题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...

随机推荐

  1. 安卓开发学习日记 DAY1

    1.eclipse安装,很简单 2.安卓sdk manager 下载安装 sdk manager是一个安卓开发所使用的sdk文件的管理程序,可以使用这个程序在官网上下载相应的安卓的api等.因为需要在 ...

  2. 7.1 java 类、(成员)变量、(成员)方法

    /* * 面向对象思想: * 面向对象是基于面向过程的编程思想. * * 面向过程:强调的是每一个功能的步骤 * 面向对象:强调的是对象,然后由对象去调用功能 * * 面向对象的思想特点: * A:是 ...

  3. 基于 HTML5 WebGL 的楼宇智能化集成系统(三)

    前言       2018年7月,信息化部印发了<工业互联网平台建设及推广指南>和<工业互联网平台评价方法>,掀起了 工业互联网 的浪潮,并成为热词写入了报告中.同为信息发展下 ...

  4. 学习Salesforce | Einstein业务机会评分怎么玩

    Einstein 业务机会评分(Opportunity Scoring)是销售团队的得力助手,通过分数以及研究影响分数的因素,确定业务机会的优先级,赢得更多交易. Einstein 业务机会评分可以给 ...

  5. 5个有趣的Python小知识,结果令人意外

    1 字符串驻留 如果上面例子返回True,但是下面例子为什么是False: 这与Cpython 编译优化相关,行为称为字符串驻留,但驻留的字符串中只包含字母,数字或下划线. 2 相同值的不可变对象 这 ...

  6. 爬虫的新手使用教程(python代理IP)

    前言 Python爬虫要经历爬虫.爬虫被限制.爬虫反限制的过程.当然后续还要网页爬虫限制优化,爬虫再反限制的一系列道高一尺魔高一丈的过程.爬虫的初级阶段,添加headers和ip代理可以解决很多问题. ...

  7. stand up meeting 1/20/2016

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云  修复bug    6 修复bug    6 foxit PDF ...

  8. 【半译】在ASP.NET Core中创建内部使用作用域服务的Quartz.NET宿主服务

    在我的上一篇文章中,我展示了如何使用ASP.NET Core创建Quartz.NET托管服务并使用它来按计划运行后台任务.不幸的是,由于Quartz.NET API的工作方式,在Quartz作业中使用 ...

  9. linux常用命令--文件和目录

    cd /home 进入 '/ home' 目录' cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd 进入个人的主目录 cd ~user1 进入个人的主目录 cd - 返回上次所在的目 ...

  10. linux CVE-2019-14287 Sudo提权漏洞

    CVE-2019-14287 sudo介绍 sudo,也就是以超级管理员身份运行(superuser do)的意思.sudo 是 Linux 中最常使用的重要实用程序之一,它功能十分强大,几乎安装在每 ...