输入一个字符串,打印出所有排列,要求没有重复:如:输入“aab”, 输出:"aab", "aba", "baa".
#include <iostream>
#include <string>
using namespace std;
bool exchangeable(string &s, int begin, int end)
{
for (int i = begin; i < end; i++)
{
if (s[i] == s[end])
{
return false;
}
}
return true;
}
void arrange(string &s, int begin)
{
if (begin == s.length())
{
cout << s << endl;
}
else
{
for (int i = begin; i < s.length(); i++)
{
if (exchangeable(s, begin, i))
{
swap(s[begin], s[i]);
arrange(s, begin+1);
swap(s[begin], s[i]);
}
}
}
}
int main()
{
string a = "addd";
arrange(a, 0);
return 0;
}