当前位置:网站首页>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;
}
}
边栏推荐
- 复现Thinkphp 2.x 任意代码执行漏洞
- 华为哈勃化身硬科技IPO收割机
- MySQL----函数
- P1451 求细胞数量/1329:【例8.2】细胞
- I spring web upload
- What are CSRF, XSS, SQL injection, DDoS attack and timing attack respectively and how to prevent them (PHP interview theory question)
- Can gbase 8A view the location of SQL statement history?
- Number protection AXB function! (essence)
- Stm32+bh1750 photosensitive sensor obtains light intensity
- 机器学习笔记 - 灰狼优化
猜你喜欢

I spring and autumn blasting-2

swiper. JS to achieve barrage effect

CPU design related notes

机器学习笔记 - 灰狼优化

"Sequelae" of the withdrawal of community group purchase from the city

Your childhood happiness was contracted by it

30岁汇源,要换新主人了

Fr exercise topic --- comprehensive question

Bugku alert
![1330: [example 8.3] minimum steps](/img/69/9cb13ac4f47979b498fa2254894ed1.gif)
1330: [example 8.3] minimum steps
随机推荐
Ionic Cordova project modification plug-in
MySQL之CRUD
Super wow fast row, you are worth learning!
Array sorting num ranking merge in ascending order
Redis distributed lock principle and its implementation with PHP (2)
漫画:程序员不是修电脑的!
R 熵权法计算权重及综合得分
Ctfshow web entry information collection
Crud de MySQL
MySQL表字段调整
Bugku alert
DVWA range clearance tutorial
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
Redis distributed lock principle and its implementation with PHP (1)
漫画:优秀的程序员具备哪些属性?
JMeter performance test: serveragent resource monitoring
Anti shake and throttling
1330:【例8.3】最少步数
mapper. Comments in XML files
[12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!