AcWing 3160. 拉马车(python)
原题链接
简单
作者:
atri_17
,
2024-03-29 20:56:44
,
所有人可见
,
阅读 5
python代码
a = list(input()) #a手牌
b = list(input()) #b手牌
queue = [] #桌上的牌
it = 0 #计数器,次数过多认为游戏无法结束
while len(a)>0 and len(b)>0:
it += 1
flag_a = True #标志本轮游戏中,a是否收牌
flag_b = True #标志本轮游戏中,b是否收牌
while flag_a: #若a收牌了,flag_a依旧等于True,对应于题目中“收牌者继续出牌”,直到a出牌后不能收牌,flag_a为False,轮到b出牌
tmp_a = a.pop(0)
if tmp_a in queue: #a能收牌
a.append(tmp_a)
while True: #从栈(桌子上的牌)顶依次拿牌到手牌最后,直到拿的牌与出的牌相同
if queue[-1] == tmp_a:
a.append(queue.pop())
break
else:
a.append(queue.pop())
else: #a不能收牌
queue.append(tmp_a)
flag_a = False
#print("a:", a)
if len(a) == 0: #如果a没牌了,直接结束游戏,b不用再出牌了
break
while flag_b:
tmp_b = b.pop(0)
if tmp_b in queue:
b.append(tmp_b)
while True:
if queue[-1] == tmp_b:
b.append(queue.pop())
break
else:
b.append(queue.pop())
else:
queue.append(tmp_b)
flag_b = False
#print("b:", b)
#print("queue:", queue)
if it>1000:
break
if it>1000:
print(-1)
else:
if len(a)>0:
print("".join(a))
else:
print("".join(b))