【发布时间】:2021-05-24 21:37:19
【问题描述】:
此函数打印字符串的排列,如何修改它以使其仅打印唯一的排列? “没有重复”
void RecPermute(string soFar, string rest) {
if (rest == "") // No more characters
cout << soFar << endl; // Print the word
else // Still more chars
// For each remaining char
for (int i = 0; i < rest.length(); i++) {
string next = soFar + rest[i]; // Glue next char
string remaining = rest.substr(0, i) + rest.substr(i + 1);
RecPermute(next, remaining);
}
}
// "wrapper" function
void ListPermutations(string s) {
RecPermute("", s);
【问题讨论】:
-
将排列保存到
std::unordered_set,然后打印集合。 -
代码被错误地重复了两次。代码以包装函数结束
标签: c++ string permutation