
假设我们有一个小写字符串S,它包含n个字符。我们需要找到两个非空的 子串P和Q,使得−
P和Q都是S的子序列
对于每个索引i,S[i]属于P和Q中的一个且仅属于一个。
P尽可能地按字典顺序最小。
所以,如果输入是S = "thelightsaber",那么输出将是10,因为我们需要2个红色的
笔记本,3个绿色笔记本和5个蓝色笔记本。
立即学习“C++免费学习笔记(深入)”;
为了解决这个问题,我们将按照以下步骤进行:
c := S sort the array c a := position of (c[0]) in S delete c from S print c[0] and S
让我们看下面的实现以更好地理解−
#include <bits/stdc++.h>
using namespace std;
void solve(string S){
string c = S;
sort(c.begin(), c.end());
int a = S.find(c[0]);
S.erase(S.begin() + a);
cout << c[0] << ", " << S << endl;
}
int main(){
string S = "thelightsaber";
solve(S);
}"thelightsaber"
a, thelightsber
以上就是C++代码来找到具有一个最小子字符串的两个子字符串的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号