题解 | #火车进站#
火车进站
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109
def xx(a, b, c):
if not a and b: # 只有进站
return xx(a + b[:1], b[1:], c)
if a and b: #有进站和出站
b1 = b.copy() #注意列表的直接传递是会相互影响的 需要copy()
return xx(a + b[:1], b[1:], c) + xx(a[:-1], b1, c + a[-1])
if a and not b: #只有出站
for i in a[::-1]:
c += i
return [c]
while True:
try:
n = int(input())
b = input().split()
for i in sorted(xx([], b, "")):
for j in i:
print(j, end=" ")
print("")
except:
break
