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. CodeForces - 1316B String Modification(找规律)

    题目链接:点击查看 题目大意:给出一个字符串 s ,需要求出一个 k ,满足 i ∈ [ 1 , n - k + 1 ]中,每个s[ i : i + k - 1 ]都反转一遍,使得最后得到的字符串字典 ...

  2. CodeCraft-20 (Div. 2)B.String Modification

    B. String Modification time limit per test1 second memory limit per test256 megabytes inputstandard ...

  3. Codeforces Round #715 (Div. 1) B. Almost Sorted 找规律

    传送门 文章目录 题意: 思路: 题意: 思路: 找规律yydsyydsyyds. 一看没什么想法,所以打了个表,好家伙,不打不知道,一打不得了,下面是n=6n=6n=6的符合要求的情况: 不难发现, ...

  4. 1031 Hello World for U (20 分)【难度: 一般 / 知识点: 找规律】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805462535356416 #include<bits/stdc+ ...

  5. 第十八章 20结构体与string

    // 20结构体与string //string是一个类,在C++中类与结构体区另不大,因此string类与结构体的区别也不大,既然string类结构体的区别不大,那么我们可以把一个结构传递给函数并返 ...

  6. String Modification CodeForces - 1316B(规律)

    Vasya has a string s of length n. He decides to make the following modification to the string: Pick ...

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

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

  8. el表达式找不到属性的错误修改方法(`javax.el.PropertyNotFoundException: 类型[java.lang.String]上找不到属性[book])

    el表达式找不到属性的错误修改方法 报错信息: javax.el.PropertyNotFoundException: 类型[java.lang.String]上找不到属性[book] 在网上查找了很 ...

  9. Educational Codeforces Round 87 (Rated for Div. 2) C1+C2(几何图形/找规律)

    C1传送门 C2传送门 给出一个正2n边形,求出其最小外接正方形的边长 当n为偶数时 不难发现正多边形刚好有四条边和正方形的四条边重合,观察发现该正多边形的边长刚好为内接圆的直径 当n为奇数时 第一眼 ...

最新文章

  1. 如何评价CVPR 2021的论文接收结果?
  2. largest number java_将给定的数组组成最大的数 Largest Number
  3. oracle SQL性能优化
  4. 4行关键代码实现灰色模型GM(1, 1)
  5. 脑与认知科学2 脑神经电生理学下
  6. leetcode-寻找两个正序数组的中位数
  7. mysql 选择字符集 拉丁字符集_mysql的字符集
  8. 如何在iPhone和iPad上允许“不受信任的快捷方式”
  9. 在 PostgreSQL 中使用码农很忙 IP 地址数据库
  10. React开发(274):ant design table额外展开行
  11. js图片转二进制流_为了安全问题后台返回前端得文件为二进制得流,我们该如何处理这些流文件...
  12. extjs grid显示html,Extjs grid column里添加button等html标签,并增加点击事件
  13. Ubuntu gerrit 安装配置
  14. MTK平台调试LCD步骤浅析
  15. 计算机u启动无法识别,电脑不识别u盘启动盘
  16. 信用报告OCR服务 助力授信审核快人一步
  17. 触动精灵 获取getColor颜色失败
  18. 拼音加加符号编码列表
  19. 利用计算机技术分析学生成绩,基于数据挖掘技术的学生成绩分析
  20. 安卓-无敌解决ListView添加标题头无法正常显示的问题(歪门邪道)

热门文章

  1. 安装配置好openstack环境的虚拟机,须要改动ip时,在数据库中同步改动ip的方法...
  2. 系统运维篇之HP-DL580-Linux配置Bonding网卡组实例
  3. 如何选择IDC服务器托管服务商
  4. 网页制作技术革新:《HTML5 网站大观》系列文章导航
  5. PowerDesigner(1)----转载
  6. 部署App-V Client,应用程序虚拟化体验系列之三
  7. Mocha BSM产品亮点——以Portal为展现中心的监控管理平台
  8. Shell 命令大全Xhell入门
  9. pythonos pathjson_python进阶05 常用问题库(1)json os os.path模块
  10. 基于UML的面向对象分析与设计