题解 | #火车进站#
火车进站
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109
num = int(input())
l_train = input().split()
res = []
def station(insta, outsta, wait):
# print(insta, outsta, wait)
if len(insta) == 0 and len(wait) != 0:
# must put train in
station(insta + [wait[0]], outsta, wait[1:])
elif len(wait) == 0 and len(insta) != 0:
# must pick train out
station(insta[:-1], outsta + [insta[-1]], wait)
elif len(insta) != 0 and len(wait) != 0:
# option to put in or pick out
station(insta[:-1], outsta + [insta[-1]], wait)
station(insta + [wait[0]], outsta, wait[1:])
elif len(insta) == 0 and len(wait) == 0:
res.append(' '.join(outsta))
insta, outsta = [], []
station(insta, outsta, l_train)
for i in sorted(res):
print(i)