B. String Modification
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vasya has a string ss of length nn. He decides to make the following modification to the string:
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:
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
Copy
6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p
output
Copy
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 :
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
手写一下样例不难发现,交换k次就是把原字符串的前缀后缀重新拼接,注意交换次数为奇数的时候还需要翻转前缀
- # include
- # include
- # include
-
- using namespace std;
-
-
- int main ()
- {
-
- int t;
-
- cin>>t;
-
- while(t--)
- {
- int n;
-
- cin>>n;
-
- string s;
- cin>>s;
-
- string minn=s;
- int ans=1;
-
- string pree="";
- pree+=s[0];
- int pos=1;
-
- for(int i=1; i
- {
- string now;
-
-
- int cnt=n-i;
- if((cnt)%2)
- {
- string bac=s.substr(i);
-
- now=bac+pree;
-
-
- }
- else
- {
-
- string pre=s.substr(0,i);
-
- string bac=s.substr(i);
-
-
-
- now=bac+pre;
-
-
-
- }
-
- pree=s[pos]+pree;
- pos++;
-
-
- if(now
- {
- ans=i+1;
- minn=now;
- }
- }
-
-
- cout<
-
- }
-
- return 0;
- }
-
相关阅读:
Java练习题2020-4
从面向对象解读设计思想
携手武重集团共建新营销一体化平台,助推央企迈向世界一流
[附源码]Python计算机毕业设计Django基于VUE的网上订餐系统
dev C++5.11的使用技巧:调试、快捷键等(备战蓝桥杯)
算法题: 数组topK问题-总结
Pinia 是什么?Redux、Vuex、Pinia 的区别?
在单链表中删除所有值为x的结点
HTML5+CSS3+JavaScript 实现按键令小女孩移动,改变动画效果
[网络工程师]-传输层协议-UDP协议
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126300895