当前位置:网站首页>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;
}
}
边栏推荐
- lvgl 显示图片示例
- CSRF, XSS science popularization and defense
- 复现Thinkphp 2.x 任意代码执行漏洞
- Number protection AXB function! (essence)
- Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
- Cartoon: what are the attributes of a good programmer?
- Common MySQL interview questions (1) (written MySQL interview questions)
- Interpretation of Apache linkage parameters in computing middleware
- First PR notes
- 如何将 DevSecOps 引入企业?
猜你喜欢
SQL Server learning notes
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
CODING DevSecOps 助力金融企业跑出数字加速度
[JVM] operation instruction
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
qt creater断点调试程序详解
Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
Install and configure Jenkins
Garbage collection mechanism of PHP (theoretical questions of PHP interview)
随机推荐
【jvm】运算指令
MySQL5.7的JSON基本操作
Can I pass the PMP Exam in 20 days?
把 ”中台“ 的思想迁移到代码中去
Redis' transaction mechanism
12 MySQL interview questions that you must chew through to enter Alibaba
mapper.xml文件中的注释
Bugku easy_ nbt
Aike AI frontier promotion (7.5)
Detailed explanation of QT creator breakpoint debugger
sql server学习笔记
go学习 ------jwt的相关知识
sql server char nchar varchar和nvarchar的区别
Brief introduction of machine learning framework
Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
"Sequelae" of the withdrawal of community group purchase from the city
Reconnaissance des caractères easycr
Fr exercise topic --- comprehensive question
Bugku's Ah Da
JS topic - console log()