Official
C - 化学反応シミュレーション / Chemical Reaction Simulation Editorial
by
C - 化学反応シミュレーション / Chemical Reaction Simulation Editorial
by
sounansya
スタックに追加する操作をシミュレーションしていけば良いです。
例えば、スタックに追加する前にスタックの長さが \(1\) 以上かつスタックの末尾・今から追加しようとする元素の組が反応パターンに存在する場合はスタックから元素を取り出す、そうでない場合は元素を追加する、といった処理をすれば良いです。
n, m = map(int, input().split())
s = input()
ty = set()
for i in range(m):
x, y = map(str, input().split())
ty.add((x, y))
ans = []
for c in s:
if len(ans) >= 1 and (ans[-1], c) in ty:
ans.pop()
continue
ans.append(c)
if len(ans) == 0:
print("EMPTY")
else:
print("".join(ans))
