题解 | 吐泡泡
吐泡泡
https://www.nowcoder.com/practice/f86fa2221c094b3d8d1fc79bae450d96
class Stack: def __init__(self) -> None: self.items = [] def push(self, x): self.items.append(x) def pop(self): if not self.is_empty(): self.items.pop() def is_empty(self): return len(self.items) == 0 def peek(self): # 修正拼写 if not self.is_empty(): return self.items[-1] return None def tu_bubbles(bubbles): # 初始化栈 stack = Stack() # 遍历泡泡序列 for bubble in bubbles: if bubble == "o": if stack.peek() == "o": stack.pop() if stack.peek() == "O": stack.pop() else: stack.push("O") else: stack.push("o") elif bubble == "O": if stack.peek() == "O": stack.pop() else: stack.push("O") # 输出最终结果 return "".join(stack.items) # 读取输入 import sys n = int(input()) for line in sys.stdin: s = line.strip() print(tu_bubbles(s))