![](/img/trans.png)
[英]Given a string, how can I add only unique characters from another string to it?
[英]Given a set of characters, how can you display the string with a higher lexicographical order, only by rearranging the characters?
假設您有字符串cana
。 字典順序高於此的字符串將是cnaa
。 有什么方法可以做到這一點,比從右到左檢查每個字符更快嗎?
#include <string>
#include <algorithm>
void swap_chars(char &a, char &b) {
char m = a;
a = b;
b = m;
}
std::string next(std::string str) {
for (int i=str.length()-1; i>0; i--)
if (str.at(i-1) < str.at(i)) {
swap_chars(str.at(i-1), str.at(i));
std::sort(str.begin()+i, str.end());
break;
}
return str;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.