[20250901] BOJ / G5 / 괄호의 값 / 이준희 #789
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/2504
🧭 풀이 시간
40분
👀 체감 난이도
✏️ 문제 설명
( ) [ ] 로 이루어진 문자열에서 ()는 2, []는 3 (x)는 x2, [x]는 x3 ()[]는 2+3 이라고 할 때
주어진 문자열의 값을 구하는 문제입니다.
🔍 풀이 방법
스택을 이용해서 풀었습니다.
열린 괄호인 경우에 스택에 집어넣고 닫힌 괄호의 경우에 스택에서 꺼내어 괄호가 매칭되는지 확인하여 계산했습니다.
스택에 넣을 때 character 스택과 integer 스택을 각각 두어서 계산했습니다.
괄호가 겹쳐있는 경우 곱셈을 구현해야 했기 때문에 열린 괄호의 경우 integer 배열에 -1 삽입하여 열린괄호를 표시하고, 닫힌 괄호를 만나는 경우 스택에서 -1을 만날 때 까지 pop 하여 계산하는데 이 때 0인 경우에는 단일 괄호로 그냥 값을 더하고, 0이 아닌 경우에는 중복괄호이기 때문에 곱셈해서 더하는 방식으로 구현했습니다.
⏳ 회고
처음에 character 스택 한개만 두는 방식으로 풀려고 하다가 너무 복잡하게 되서 두개를 사용하는 방식으로 풀었습니다.