字符串的排列

#include<iostream>
#include<string>
using namespace std;

void swap(char& a,char& b)
{
    if( a == b)
        return;
    char temp = a;
    a = b;
    b = temp;
}
void permutation(string& ptr, int i);

void permutation(string& s)
{
    if(s.size() <= 0)
        return;
    permutation(s, 0);
}
void permutation(string& ptr, int i)
{
    if(i >= ptr.size())
    {
        cout << ptr << endl;    
    }
    for(int j = i;  j != ptr.size(); ++j)
    {
    
        if(ptr[i] == ptr[j] && i != j)  // 此处避免相同字符
            continue;
            
        swap(ptr[i],ptr[j]);
        {
            permutation(ptr,i+1);
            
        }
        swap(ptr[i],ptr[j]);
    }
}

int main()
{
    string s;
    cin >> s;
    permutation(s);
    return 0;
}
原文地址:https://www.cnblogs.com/yi-meng/p/3773810.html