题解 | #火车进站#

火车进站

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)

全部评论

相关推荐

不愿透露姓名的神秘牛友
01-22 18:07
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务