★ 9935 문자열 폭발 ★
import sys
input=sys.stdin.readline
s=list(input().rstrip())
explode=list(input().rstrip())
explode_length=len(explode)
ans=[]
for x in range(len(s)):
ans.append(s[x])
if s[x] == explode[-1]:
if explode_length<=len(ans) and ans[-explode_length:] == explode:
for _ in range(explode_length):
ans.pop()
if not ans:
print('FRULA')
else:
print(*ans,sep='')
😮 폭발 문자열의 길이가 최대 36이므로, 문자열을 계속 ans에 넣다가 맨 마지막 폭발 문자열이 들어갈 경우, 앞선 들어간 문자열이 폭발 문자열이 맞다면 pop() 연산으로 빠르게 삭제. 그리고 계속 update. stack에 넣고 빼는 연산 O(1)을 활용해 시간 복잡도 최대 O(N)
'BOJ > 🥇' 카테고리의 다른 글
★Math & Geometry Advanced I - 1 Solved★ (0) | 2024.04.27 |
---|---|
★Implementation&Simluation Upper-Advanced I - 1 Solved★ (0) | 2024.03.03 |
★Two-Pointers Advanced I - 7 Solved★ (0) | 2024.02.11 |
★Implementation&Simluation Advanced I - 5 Solved★ (0) | 2024.01.28 |
★Backtracking Upper-Advanced I - 1 Solved★ (0) | 2024.01.24 |
댓글