当前位置:网站首页>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;
}
}
边栏推荐
- Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
- Cartoon: what are the attributes of a good programmer?
- SQL Server learning notes
- queryRunner. Query method
- Coding devsecops helps financial enterprises run out of digital acceleration
- Reconnaissance des caractères easycr
- Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
- sql server学习笔记
- easyOCR 字符識別
- 你童年的快乐,都是被它承包了
猜你喜欢

Bubble sort, insert sort

Install and configure Jenkins

亿咖通科技通过ISO27001与ISO21434安全管理体系认证

超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜

Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking

DVWA range clearance tutorial

Bugku's Eval

Bugku's Ah Da

计算中间件 Apache Linkis参数解读

I spring web upload
随机推荐
JMeter performance test: serveragent resource monitoring
Leetcode: Shortest Word Distance II
Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
Optional parameters in the for loop
MySQL之CRUD
Bugku's eyes are not real
Bugku cyberpunk
Brief introduction of machine learning framework
Stm32+bh1750 photosensitive sensor obtains light intensity
maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
Magic methods and usage in PHP (PHP interview theory questions)
GPS原始坐标转百度地图坐标(纯C代码)
如何将 DevSecOps 引入企业?
Easyocr character recognition
Reconnaissance des caractères easycr
Cartoon: programmers don't repair computers!
easyOCR 字符識別
Anaconda uses China University of science and technology source
市值蒸发超百亿美元,“全球IoT云平台第一股”赴港求生
P1451 求细胞数量/1329:【例8.2】细胞