当前位置:网站首页>F. Min cost string problem solving Report
F. Min cost string problem solving Report
2022-07-05 15:25:00 【wch(】
F. Min Cost String Problem solving report
label : character string structure
subject

The question :
Give constant n,k, Let's use before k Lowercase letters are constructed with a length of n String
Letters can be in front k Any use or no use within the scope , But it is required to minimize the index pairs in the output string
When s[i]=s[j] And is s[i+1]=s[j+1] (1≤i<j<|s|) It constitutes an index pair
Their thinking :
To minimize index pairs , We can divide the string into one-to-one pairs ,
such as s=aaba At this time, we see three pairs aa ab ba It has been taken
Then the next idea is to list all Character pair Add to s in
If the string length is greater than or equal to n Before output n Characters
If the length of the character string is less than n
Because all the character pairs that can be formed are already in s It's in No matter what you add, there will be index pairs
We let s += s Until the length is greater than n Before interception n Character output
resize Function usage
When n The length is less than the string s The length of s.resize(n), Intercept s front n Characters
When n The length is greater than the string s The length of char a; s.resize(n,a), stay s Add characters after a until s Length up to n
Code implementation
#include <iostream>
#include <string>
using namespace std;
int main(){
long long n,k;
cin>>n>>k;
string s("");
for(int i=0;i<k;i++){
s+='a'+i;
for(int j=i+1;j<k;j++)
{
s+='a'+i;
s+='a'+j;
}
}
while(s.size()<n)s+=s;
s.resize(n);
cout<<s<<endl;
}
边栏推荐
- 我想咨询一下,mysql一个事务对于多张表的更新,怎么保证数据一致性的?
- mapper. Comments in XML files
- CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems
- sql server char nchar varchar和nvarchar的区别
- Common MySQL interview questions (1) (written MySQL interview questions)
- Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
- ICML 2022 | explore the best architecture and training method of language model
- [JVM] operation instruction
- Talk about your understanding of microservices (PHP interview theory question)
- B站做短视频,学抖音死,学YouTube生?
猜你喜欢
随机推荐
qt creater断点调试程序详解
Bugku's steganography
Appium自动化测试基础 — APPium基础操作API(二)
Common MySQL interview questions
CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems
想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
Cartoon: programmers don't repair computers!
[recruitment position] Software Engineer (full stack) - public safety direction
市值蒸发超百亿美元,“全球IoT云平台第一股”赴港求生
Common redis data types and application scenarios
Your childhood happiness was contracted by it
MySQL之CRUD
Leetcode: Shortest Word Distance II
P6183 [USACO10MAR] The Rock Game S
Ctfshow web entry command execution
数据库学习——数据库安全性
episodic和batch的定义
ionic cordova项目修改插件
你童年的快乐,都是被它承包了
Cartoon: what are the attributes of a good programmer?









