题解 | #火车进站#

火车进站

http://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109

利用“进出”操作的排列

while True:
	try:
		n = int(input())
		trains = input().split()
		perm = [[] for _ in range(n+1)]
		perm[1] = ['ab']

		for i in range(2, n+1):
			for x in perm[i-1]:
				for j in range(len(x)):
					sub = list(x)
					sub.insert(j, 'a')
					sub.insert(j+1, 'b')
					sub = ''.join(sub)
					if not sub in perm[i]:
						perm[i].append(sub)

		res = []
		for pe in perm[n]:
			stack = []
			t = 0
			out = ''
			for c in pe:
				if c == 'a':
					stack.append(trains[t])
					t += 1
				else:
					out += stack.pop()
			res.append(out)
		res.sort()
		res = [' '.join(list(r)) for r in res]
		for r in res:
			print(r)
	except:
		break
全部评论

相关推荐

过往烟沉:我说什么来着,java就业面就是广!
点赞 评论 收藏
分享
10-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务