题解 | #全排列#
全排列
http://www.nowcoder.com/practice/5632c23d0d654aecbc9315d1720421c1
递归方式
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
string seq;
const int MAXN = 10;
bool visit[MAXN];
void digui(const string &s, int index){//index指当前递归深度,从0开始
if(index == s.size()){//递归出口
printf("%s\n",seq.c_str());
seq.erase(seq.end()-1);
return;
}
for(int i=0; i<s.size(); i++){
if(!visit[i]){
visit[i] = true;
seq.push_back(s[i]);
digui(s, index+1);
visit[i] = false;
}
}
//把后面的扫完了也得回退
if(seq.size()){
seq.erase(seq.end()-1);
return;
}
}
int main(){
string s;
while(cin >> s){
sort(s.begin(),s.end());
memset(visit, false, sizeof(visit));
digui(s, 0);
s.clear();
seq.clear();
}
return 0;
}