@
tsuibin 每次记录一下被交换的元素即可:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void permutation_helper(string &str, int start, int end) {
if (start == end) {
cout << str << endl;
} else {
bool visited[256];
fill(visited, visited + 256, false);
for (int i = start; i <= end; ++i) {
if (!visited[str[i]]) {
swap(str[start], str[i]);
permutation_helper(str, start + 1, end);
swap(str[start], str[i]);
visited[str[i]] = true;
}
}
}
}
void permutation(string str) {
sort(str.begin(), str.end());
permutation_helper(str, 0, str.length() - 1);
}
int main() {
string s("aab");
permutation(s);
return 0;
}