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 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;
}
}
- ASM 翻译系列第四十弹:理解ASM中 REQUIRED_MIRROR_FREE_MB和USABLE_FILE_MB的含义
- 给XFN链接关系加上“nofollow”选项
- LVS中采用Jboss作为RealServer的配置要点
- 基于Prometheus的数据库监控
- Uva-------(11462) Age Sort(计数排序)
- hdu----(2848)Repository(trie树变形)
- 【MySQL】主从GTID复制修复
- hdu---(1800)Flying to the Mars(trie树)
- 解决Tomcat数据连接池无法释放
- hdu----(1075)What Are You Talking About(trie之查找)
- hdu--(1247)Hat’s Words(trie树)
- HDU----(4519)郑厂长系列故事——体检
- 张戈博客成功启用移动端主题:Mobile Pack
- Oracle数据库性能障碍分析利器:SYSTEMSTATE DUMP介绍
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- GitLab 初次搭建使用教程
- Vue中鼠标事件
- gitlab添加ssh-keys之后克隆依然需要输入密码
- VM虚拟机系统自定义分区
- k8s问题记录
- Hexo博客Next主题浏览统计不显示
- 视频上云网关平台EasyCVR使用海康SDK拉流协议分析
- 计算机网络物理层习题
- 从数据库中查询马上过生日的人并统计各年龄段及性别所占的人数
- Ubuntu19.10 中安装 JDK
- 在 Ubuntu19.10 上安装 wine 并安装 QQ 等软件
- wordpress迁移至hugo及其自动化发布文章全记录
- 视频流媒体平台EasyNVR硬件设备使用华科云arm版如何修改为固定IP?
- 详解 IP 地址
- k8s解决pod调度不均衡的问题