[英]Given a set of characters, how can you display the string with a higher lexicographical order, only by rearranging the characters?
Let's say you have the string cana
.假设您有字符串
cana
。 The string with the lexicographical order higher than that, would be cnaa
.字典顺序高于此的字符串将是
cnaa
。 Is there any way you could do that, faster than just checking from right to left for every character?有什么方法可以做到这一点,比从右到左检查每个字符更快吗?
#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.