String Modification (CodeCraft-20 (Div. 2))
题意描述
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
思路
还是一题选手,这道题一开始想的是暴力的做法,但数据范围太大,所以只能找规律,但找了很长时间,发现规律找错了,没有从奇偶方面下手。这道题的规律是,如果从k开始的字符串长度是奇数,就将k前的字符串翻转。(还是做题太少)
AC代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<string>
#include<vector>
#include<map>
#define IOS ios::sync_with_stdio(false); cin.tie(0);
using namespace std;
typedef pair<string,int> PII;
typedef long long ll;
const int N=5050;
const int INF=0x3f3f3f3f;
int main(){
IOS;
int T;
cin>>T;
while(T--){
int n;
string str;
cin>>n>>str;
PII t=make_pair(str,1);
for(int i=2;i<=n;i++){
string s=str.substr(i-1),u=str.substr(0,i-1);
if((n-(i+1))%2){
reverse(u.begin(),u.end());
}
t=min(make_pair(s+u,i),t);
}
cout<<t.first<<endl<<t.second<<endl;
}
return 0;
}
- 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 数组属性和方法
- Mysql5.6版本新增列,执行DDL的时候竟然锁表了,这应该是是最好的解决办法之一了!!
- spring之bean的作用域
- spring之spring表达式语言:SpEL
- 反欺诈模型(数据不平衡)
- 基于TypeScript封装Axios笔记(一)
- 一次性清除“知乎”所有DIV
- spring之bean之间的关系
- spring之未加后置处理器的bean的生命周期
- linux之在centos7中配置java开发环境
- 秒懂java规则表达式框架Aviator2.3.0
- 细品Redis高性能数据结构之SDS
- linux之第一个shell程序
- pyspark之从HDFS上读取文件、从本地读取文件
- spring之使用外部属性文件(连接数据库时使用)
- 【python-leetcode92-翻转链表】反转链表2