package ccnu.allTest;
import java.util.ArrayList;
import java.util.Stack;
public class PrintPopSerial {
public static void main(String[] args) {
ArrayList<String> pops = popSerials(new char[]{'a', 'b', 'c', 'd'}, 0);
int i = 0;
for(String pop : pops){
i++;
System.out.println("第" + i + "个: " + pop);
}
}
public static ArrayList<String> popSerials(char[] push, int begin){
return popSerials(push, begin, new Stack<Character>(), new StringBuilder());
}
private static ArrayList<String> popSerials(char[] push, int begin, Stack<Character> s, StringBuilder sb){
ArrayList<String> pops = new ArrayList<String>();
if(push == null){
return null;
}
s.push(push[begin]);
begin++;
if(begin == push.length){
while(!s.isEmpty()){
sb.append(s.pop());
}
pops.add(sb.toString());
}else{
pops.addAll(popSerials(push, begin, (Stack<Character>)s.clone(), new StringBuilder(sb)));
while(!s.isEmpty()){
sb.append(s.pop());
pops.addAll(popSerials(push, begin, (Stack<Character>)s.clone(), new StringBuilder(sb)));
}
}
return pops;
}
}