题解 | #火车进站#
火车进站
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109?tpId=37&tqId=21300&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D13&difficulty=undefined&judgeStatus=undefined&tags=&title=
N = int(input()) num = list(input().split(" ")) # 本质是回溯法 def dfs(num, i, stack, out): if i >= len(num) and len(stack) == 0: ans.append(" ".join(out)) return # 火车入站 if i < len(num): stack.append(num[i]) dfs(num, i+1, stack, out) stack.pop() # 火车出站 if len(stack) > 0: out.append(stack.pop()) dfs(num, i, stack, out) stack.append(out.pop()) ans = [] dfs(num, 0, [], []) for s in sorted(ans): print(s)