Description
设R={ r1, r2, ……, rn
给定n以及待排列的n个元素。计算出这n个元素的所有不同排列。
Input
输入数据的的第1行是元素个数n,1≤n≤500。接下来的1行是待排列的n个元素。
Output
将计算出的n个元素的所有不同排列输出,每种排列占1行,最后1行中的数是排列总数。
Sample Input
4 aacc
Sample Output
aacc
acac
acca
caac
caca
ccaa
6
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long int ll;
ll ans = 0;
int N;
char a[505];
bool is_ok(int start,int end,char ch){
for(int i=start; i<end; i++)
if(a[i]==ch)
return 0;
return 1;
}
void solve(int st,int ed){
if(st==ed){
ans++;
for(int i=0; i<N; i++)
cout << a[i];
cout << endl;
}
for(int i=st; i<=ed; i++)
if(is_ok(st,i,a[i])){
swap(a[st],a[i]);
solve(st+1,ed);
swap(a[st],a[i]);
}
}
int main(){
scanf("%d",&N);
scanf("%s",a);
solve(0,N-1);
printf("%I64d\n",ans);
return 0;
}