输入一个字符串,要求输出字符串中字符所有的排列,例如输入"abc",得到"abc","acb","bca","bac","cab","cba"

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

vector<string> result;

void permute(string& str, int depth, int n){
    if(depth == n){
         result.push_back(str);
        return ;
    }
    for(int i = depth; i< n; i++){
        swap(str[depth],str[i]);
        permute(str, depth+1, n);
        swap(str[depth],str[i]);
    }
}

int main(){
    string str;
    cin>>str;
    permute(str, 0, str.size());
    for(int i = 0; i < result.size(); i++){
        cout << result[i] << endl;
    }
     return  0;
}

你可能感兴趣的:(输入一个字符串,要求输出字符串中字符所有的排列,例如输入"abc",得到"abc","acb","bca","bac","cab","cba")