AcWing 1537. 递归实现排列类型枚举 II
原题链接
简单
作者:
henhen敲
,
2020-06-16 22:36:00
,
所有人可见
,
阅读 676
import java.io.*;
import java.util.*;
class Main{
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int nextInt()throws Exception{
in.nextToken();
return (int)in.nval;
}
static int[] a, path;
static int n;
static void DFS(int d, int st){
if(d>=n){
for(int i=0; i<n; i++) out.print(path[i]+" ");
out.println("");
return;
}
for(int i=0; i<n; i++){
if(i>0&&((st>>(i-1))&1)==0&&a[i]==a[i-1]) continue;
if(((st>>i)&1)==0){
path[d] = a[i];
DFS(d+1, st+(1<<i));
}
}
}
public static void main(String[] args)throws Exception{
n = nextInt();
a = new int[n];
path = new int[n];
for(int i=0; i<n; i++) a[i] = nextInt();
Arrays.sort(a);
DFS(0, 0);
out.close();
}
}