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;
}
}
- kvm虚拟化管理平台WebVirtMgr部署-完整记录(安装ubuntu虚拟机)-(5)
- 从MapX到MapXtreme2004[9]-标注的强调显示
- 【第一季】Vue2.0内部指令
- 从MapX到MapXtreme2004[9]-标注的强调显示
- 分布式监控系统Zabbix-3.0.3-完整安装记录(4)-解决zabbix监控图中出现中文乱码问题
- 常用Lambda表达式实例
- centos6.8部署vnc服务
- linux下的缓存机制及清理buffer/cache/swap的方法梳理
- 分组合计且排序和显示名称
- silverlight动态读取txt文件/解析json数据/调用wcf示例
- Junit加载Spring容器作单元测试_添加事务回滚
- 实现三遍决策树,你就会想出更快的算法!
- 将一段复杂文本变成字符串的赋值语句
- Linux下squid代理缓存服务环境部署
- 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 数组属性和方法
- PHP中的访问修饰符简单比较
- PHP asXML()函数讲解
- PhpStorm配置Xdebug调试的方法步骤
- python字典的值可以修改吗
- 使用Keras构造简单的CNN网络实例
- Python接口测试环境搭建过程详解
- 使用已经得到的keras模型识别自己手写的数字方式
- 解决keras使用cov1D函数的输入问题
- PHP PDOStatement::getColumnMeta讲解
- PHP PDOStatement::nextRowset讲解
- PHP _construct()函数讲解
- PHP PDOStatement::rowCount讲解
- Python 如何对文件目录操作
- Python字符串split及rsplit方法原理详解
- Laravel框架文件上传功能实现方法示例