9935-g4 성공

This commit is contained in:
2026-03-18 14:00:10 +09:00
parent 32616725c5
commit b0ec14828b

View File

@@ -0,0 +1,94 @@
# 문자열 폭발
import sys
input = sys.stdin.readline
def solution():
s = input().rstrip()
stack = []
result = ""
bomb = input().rstrip()
for ch in s:
stack.append(ch)
if stack[-1] == bomb[-1]:
idx = 0
while len(stack) >= len(bomb) and idx < len(bomb):
if stack[-1-idx] != bomb[-1-idx]:
result += "".join(stack)
stack = []
break
idx += 1
if idx == len(bomb):
for _ in range(len(bomb)):
stack.pop()
result += "".join(stack)
if not result:
print("FRULA")
else:
print(result)
return
solution()
"""
걸린 시간: 34분
시간 복잡도: 한 문자당 상수번 다뤄지기 때문에 O(n)이다.
해설: 처음에는 bomb 문자열의 순서와 쌍을 기록하는 방식으로 연결관계를 계속 파악하려고 했다. 그렇다보니 조건 분기가 많아졌고,
틀렸을 때는 어디가 잘못됐을지 감도 안 잡혀서 과감하게 다른 풀이로 전향했다.
좀 더 생각을 해보니 문자를 볼때 bomb의 마지막 문자일때만 이미 들어갔던 것들을 쭉 보고 만약에 bomb이 아니라면
지금까지 본 문자열은 앞으로도 절대 폭발할 수 없다는 것을 깨달았다.
따라서 방금 과정을 진행한 후 폭발하지 않았다면 stack에 있는 모든 문자를 다시는 보지 않기 위해 stack에서 빼고 문자열로 바꿔놓는다.
만약 폭발했다면 stack에 남아있는 것들이 또 연결되어 폭발할 수 있으므로 남겨놓는다.
"""
# def solution(): # 12:25~ 1:15 실패
# s = input().rstrip()
# bomb = {ch: i+1 for i, ch in enumerate(input().rstrip())} # start 1~
# # print(bomb)
# dq = deque([])
# group = 0
# no_group = -1
# for i, ch in enumerate(s):
# # b = -1
# bomb_order = bomb.get(ch, -1)
# if bomb_order == 1:
# group += 1
# dq.append((bomb_order, group, i))
# # b = 1
# elif not dq:
# dq.append((bomb_order, no_group, i))
# # b = 2
# elif bomb_order == len(bomb) and dq[-1][0] == len(bomb)-1:
# now_group = dq[-1][1]
# while dq and dq[-1][1] == now_group:
# dq.pop()
# # b = 3
# elif bomb_order == dq[-1][0]+1:
# dq.append((bomb_order, dq[-1][1], i))
# # b = 4
# else:
# dq.append((bomb_order, no_group, i))
# # b = 5
# # print(b, dq)
# result = ""
# while dq:
# result += s[dq.popleft()[2]]
# if not result:
# print("FRULA")
# else:
# print(result)
# return
# solution()