6.3w+
社区成员
public class stackout{
private long fact(int n)
{
if(n == 0)
return 1;
return fact(n - 1)*n;
}
private final long M(final int n)
{
return (fact(2*n))/((n + 1)*fact(n)*fact(n));
}
private static char allchar[] = {'1','1', '2','3','4'};//'a'+7,'a','b'+7,'b','c'+7,'c',
//'d'+7,'d','e'+7,'e'};
private static final int N = allchar.length;
private char outchar[] = new char[N];
private boolean op[] = new boolean[2*N];
private int count = 0;
private String[] out_st = new String[(int) M(N)];
public void k(int all, int in, int out)
{
if(all > 0)
{
op[out] = true;
k(all - 1, in + 1, out + 1);
if(in > 0)
{
op[out] = false;
k(all, in - 1, out + 1);
}
}else{
out_st[count] = "";
boolean repeat = false;
int spos = 0;
int cpos = 0;
for(int i = out; i < 2*N; i++)
{
op[i] = false;
}
// 打印出栈顺序
for (int i=0; i< N*2; i++) {
if (op[i])
{
outchar[spos++] = allchar[cpos++];
}
else {
out_st[count] += outchar[--spos];
//System.out.print(outchar[--spos]);
}
}
for(int i = 0; i < count; i++)
{
if(out_st[count].equals(out_st[i]))
{
repeat = true;
break;
}
}
if(!repeat)
{
System.out.print((1+count) + ",");
System.out.print(out_st[count]);
System.out.print("\n");
count++;
}
}
}
public static void main(String[] args){
stackout test = new stackout();
//System.out.println(test.M(11));
test.k(N, 0, 0);
}
}