题解 | #火车进站#
火车进站
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109
ln = [] def fun(station, lsin, lsout): if len(lsin) == 0: while len(station) > 0: lsout.append(station.pop()) ln.append(lsout) return if len(station) == 0: station3 = [] station3.append(lsin[0]) return fun(station3, lsin[1:], lsout[:]) station1 = station[:-1] station1.append(lsin[0]) lsout1 = lsout[:] lsout1.append(station[-1]) lsin1 = lsin[1:] fun(station1, lsin1, lsout1) station2 = station[:] station2.append(lsin[0]) lsout2 = lsout[:] lsin2 = lsin[1:] fun(station2, lsin2, lsout2) station3 = station[:-1] lsout3 = lsout[:] lsout3.append(station[-1]) lsin3 = lsin[:] fun(station3, lsin3, lsout3) return N = int(input()) ls = input().split() fun([], ls, []) ln = list({tuple(i) for i in ln}) ln.sort(reverse=True) while len(ln) > 0: print(" ".join(ln.pop()))