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.
思路:一开始以为是个思维题,但是按照思路写了一发wa到第4个样例了。。看着数据量不大,开始考虑暴力一下。所谓的暴力就是将所有的情况都考虑一遍。但是不能模拟着反转一遍。这里找一下规律就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;string s;
int n;inline string fcs(int k)
{if(k==n){reverse(s.begin(),s.end());return s;}if((n-k+1)%2==0){string o1=s.substr(k-1);string o2=s.substr(0,k-1);return o1+o2;}string o1=s.substr(k-1);string o2=s.substr(0,k-1);reverse(o2.begin(),o2.end());return o1+o2;
}
int main()
{int t;scanf("%d",&t);while(t--){cin>>n>>s;string ss=s;int _min=1;for(int k=1;k<=n;k++){string sj=fcs(k);//cout<<sj<<endl;if(sj<ss) ss=sj,_min=k;}cout<<ss<<endl<<_min<<endl;}return 0;
}

努力加油a啊,(o)/~

String Modification CodeForces - 1316B(规律)相关推荐

  1. Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)

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

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

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

  3. CodeForces - 1316B String Modification(找规律)

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

  4. Binary String Reconstruction CodeForces - 1352F(思维+构造)

    For some binary string s (i.e. each character si is either '0' or '1'), all pairs of consecutive (ad ...

  5. Codeforce Flea CodeForces - 32C 规律|思维

    题意 给我们一个n*m的表格 告诉我们每次跳跃的格子数量s 只能垂直或者水平跳跃 可以跳跃无限次 那么某格子能到达的不同格子的数量就是这个格子的值 求表格中值最大的格子的数量 分析 表面上看像是搜索 ...

  6. String Typing CodeForces - 954B

    题意: 给一个字符串,可以复制某一段字符,问最少需要多少步能将其输出,比如abcabcd,先输入abc然后再赋值abc再输入d就只需要5步. 复制的这段字符 必须是从字符串的0位置开始复制的  而且只 ...

  7. Yet Another Counting Problem CodeForces - 1342C(规律+前缀和)

    You are given two integers a and b, and q queries. The i-th query consists of two numbers li and ri, ...

  8. Binary String Minimizing CodeForces - 1256D(贪心)

    You are given a binary string of length n (i. e. a string consisting of n characters '0' and '1'). I ...

  9. A Perfectly Balanced String?(Codeforces Round #785 (Div. 2))

    A Perfectly Balanced String? Let's call a string s perfectly balanced if for all possible triplets ( ...

最新文章

  1. Ali RocketMQ与Kafka对照
  2. 魅族的android m l,Android M 外部存储剖析
  3. xp系统vba服务器,VBA获取操作系统的版本号(支持windows xp,windows 2003 ,win7 ,win10)
  4. 百胜软件2016年双十一项目正式启动
  5. Windows XP中防火墙后如何实现共享(图)
  6. Java中方法的继承以及父类未被子类覆盖的方法调用的问题
  7. 【渝粤教育】电大中专常见病药物治疗作业 题库
  8. OD反汇编EXE添加一个启动时的消息框
  9. (1)JavaScript入门
  10. 深度剖析python_汉诺塔问题深度剖析(python实现)
  11. (转载)Ubuntu如何进入图形界面
  12. 【MySQL】5.7新特性之四
  13. Angr安装与使用之使用篇(二)
  14. java的注解定义map_java – MapStruct:丰富映射注释以定义自定义映射器
  15. 第十届全国大学生智能汽车竞赛获奖名单
  16. txt文档下载另存为解决
  17. idea安装教程(不是jdr包哦)
  18. C++调python(执行py文件)
  19. python模拟登录qq邮箱爬取_python模拟登录qq邮箱
  20. java连接twitter登录,twitter应用程序只认证java android与twitter4j

热门文章

  1. php远程连接403,php中出现“ HTTP 异常 403 - 禁止访问”解决方法 总结
  2. fastadmin 批量上传不成功_shopee批量上传产品步骤
  3. 4位加法器的设计代码verilog_一个简单的8位处理器完整设计过程及verilog代码
  4. python中面向对象的ui_python中面向对象
  5. 《机器学习概论》习题答案
  6. 数学知识在计算机专业中的应用,数学建模在计算机专业中的应用
  7. 菜单自定义图标_操作系统任务栏了解多少,Windows server 2008 R2自定义通知区域...
  8. c#正则匹配取出文本内容 循环输出
  9. jquery一些基本函数
  10. POJ2559 Largest Rectangle in a Histogram(单调栈)