当前位置:网站首页>String modification problem solving Report
String modification problem solving Report
2022-07-05 15:25:00 【wch(】
String Modification Problem solving report
label : character string Looking for a regular simulation
The question :
Given a length of n(1≤n≤5000 ) String , Traverse from the beginning , For each k Flip the length subsequence , Find the one with the smallest dictionary order and k Value , If there are multiple dictionaries with the smallest order , requirement k Minimum .
Their thinking
First, find out the rules through simulation
Set string 12345;
k=1 12345
k=2 2345 1
k=3 345 21
k=4 45 123
k=5 5 4321
You can find If we divide strings into pre sequence and post sequence , The inverted sequence will be the exchange position of the front and rear sequences , According to the current example, the original pre sequence follows k And decide whether to flip , In addition, the length of the previous sequence can be determined to be k-1.
Set string 123456
k=3 3456 12
k=4 456 321
From these two examples, we can find : Whether the previous sequence is reversed is determined by k And string length n Of or related to .
Code implementation
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string s,ans;
int t;
cin>>t;
while(t--){
int n;
cin>>n;
cin>>s;
ans=s;
int k=1;
for(int i=2;i<=n;i++){
string s1,s2,s3;
s1=s.substr(0,i-1);
s2=s.substr(i-1);// from i-1 Until the position of is taken back
if((n-i)%2==0) reverse(s1.begin(),s1.end());
s3=s2+s1;
if(s3<ans){
k=i;
ans=s3;
}
}
cout<<ans<<endl;
cout<<k<<endl;
}
}
边栏推荐
- JMeter performance test: serveragent resource monitoring
- Common redis data types and application scenarios
- Usage and usage instructions of JDBC connection pool
- 可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
- 超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
- I spring and autumn blasting-2
- JS topic - console log()
- lvgl 显示图片示例
- Install and configure Jenkins
- Bubble sort, insert sort
猜你喜欢
![1330: [example 8.3] minimum steps](/img/69/9cb13ac4f47979b498fa2254894ed1.gif)
1330: [example 8.3] minimum steps

SQL Server learning notes

Mysql---- function

Redis' transaction mechanism

Detailed explanation of QT creator breakpoint debugger

Stop B makes short videos, learns Tiktok to die, learns YouTube to live?

Garbage collection mechanism of PHP (theoretical questions of PHP interview)

社区团购撤城“后遗症”

Huiyuan, 30, is going to have a new owner

Select sort and bubble sort
随机推荐
First PR notes
Want to ask the big guy, is there any synchronization from Tencent cloud Mysql to other places? Binlog saved by Tencent cloud MySQL on cos
Stm32+bh1750 photosensitive sensor obtains light intensity
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
swiper. JS to achieve barrage effect
How can the boss choose programmers to help me with development?
Using tensorboard to visualize the training process in pytoch
go学习 ------jwt的相关知识
R 熵权法计算权重及综合得分
Interpretation of Apache linkage parameters in computing middleware
机器学习笔记 - 灰狼优化
Bugku telnet
I collect multiple Oracle tables at the same time. After collecting for a while, I will report that Oracle's OGA memory is exceeded. Have you encountered it?
JS knowledge points-01
Bugku's Ping
Calculate weight and comprehensive score by R entropy weight method
mapper.xml文件中的注释
漫画:优秀的程序员具备哪些属性?
P6183 [USACO10MAR] The Rock Game S
CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems