当前位置:网站首页>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;
}
}
边栏推荐
- maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
- Garbage collection mechanism of PHP (theoretical questions of PHP interview)
- The difference between abstract classes and interfaces in PHP (PHP interview theory question)
- Ctfshow web entry explosion
- Bugku alert
- SQL Server learning notes
- OSI 七层模型
- Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification
- ICML 2022 | explore the best architecture and training method of language model
- Fr exercise topic - simple question
猜你喜欢

【jvm】运算指令

Good article inventory

qt creater断点调试程序详解

Bugku's Ah Da

No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle

swiper. JS to achieve barrage effect

Your childhood happiness was contracted by it

I spring web upload

Coding devsecops helps financial enterprises run out of digital acceleration

亿咖通科技通过ISO27001与ISO21434安全管理体系认证
随机推荐
mapper.xml文件中的注释
Talking about how dataset and dataloader call when loading data__ getitem__ () function
"Sequelae" of the withdrawal of community group purchase from the city
【jvm】运算指令
漫画:程序员不是修电脑的!
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Creation and optimization of MySQL index
Common redis data types and application scenarios
JS bright blind your eyes date selector
swiper. JS to achieve barrage effect
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
ionic cordova项目修改插件
Ten billion massage machine blue ocean, difficult to be a giant
Jmeter性能测试:ServerAgent资源监控
Mysql---- function
Install and configure Jenkins
Database learning - Database Security
Go learning ----- relevant knowledge of JWT
Creation and use of thymeleaf template
Garbage collection mechanism of PHP (theoretical questions of PHP interview)