#include <iostream>
#include <string>
#include <algorithm> // sort()、next_permutation()
using namespace std;
// (递归算法 -- 生成置换)(开发困难)
// worstTime(n) 是 O(n!)
void rec_permute(string s, unsigned k)
{
if (k == s.length() -1)
cout << s << " ";
else
for (unsigned i = k; i < s.length(); i++)
{
swap(s[i], s[k]);
rec_permute(s, k+1);
}
}
void permute(const string& s)
{
rec_permute(s, 0);
}
// (STL的通用算法 -- 生成置换)
void permutecopy(string s)
{
// 使用通用型算法 next_permutation()前必须保证有序性
sort(s.begin(), s.end());
cout << s << " ";
while (next_permutation(s.begin(), s.end()))
cout << s << " ";
}
int main()
{
string s = "ABC";
cout << "permute():\n";
permute(s);
cout << endl << endl;
cout << "permutecopy():\n";
permutecopy(s);
cout << endl;
return 0;
}