This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-08-14] dohyun #110 #122
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
|
||
풀이시간 | ||
- 약 40분 | ||
|
||
접근법 | ||
- 문자열 길이 되게 짧음 -> 시간복잡도 신경쓰지말고 꼼꼼히 구현해보자 | ||
- 이전 풀이 문제(쇠 막대기)에서 스택 풀이를 제대로 이해못했지만, 비슷한 느낌인 것 같아 쇠 막대기 풀이 참고 | ||
- 올바르지 못한 괄호열은 어떻게 정의할까? | ||
- ( 다음 ] 가 오면 땡 | ||
- [ 다음 ) 가 오면 땡 | ||
- 소괄호와 대괄호의 개수가 다르면 땡 | ||
|
||
회고 | ||
- 스택 풀이 이해하기! | ||
|
||
""" | ||
|
||
import sys | ||
|
||
text = sys.stdin.readline().strip() | ||
answer = 0 | ||
stack = [] | ||
tmp_ans = 1 # 곱하기 연산을 해야하므로 1부터 시작 | ||
|
||
def filter_correct(text): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 스택의 유효성을 검증해주는 함수를 따로 정의하신 것 굉장히 좋은 것 같습니다. |
||
cond1 = (text.count('(')==text.count(')')) & (text.count('[')==text.count(']')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외처리도 스터디 때 말씀 드렸던 것처럼 count의 시간복잡도는 O(n) |
||
cond2 = (text.count('[)') + text.count('(]') == 0) | ||
|
||
return cond1 & cond2 | ||
|
||
if filter_correct(text): | ||
for i in range(len(text)): | ||
if text[i]=="(": | ||
stack.append("(") | ||
tmp_ans *= 2 | ||
|
||
elif text[i]=="[": | ||
stack.append("[") | ||
tmp_ans *= 3 | ||
|
||
elif text[i]==")": | ||
if text[i-1]=="(": | ||
answer += tmp_ans # 괄호연산이 끝났으므로 정답 더해주기 | ||
stack.pop() | ||
tmp_ans //= 2 # 소괄호가 닫혔으므로 곱했던 2를 다시 나눠줌 | ||
|
||
elif text[i]=="]": | ||
if text[i-1]=="[": | ||
answer += tmp_ans # 괄호연산이 끝났으므로 정답 더해주기 | ||
stack.pop() | ||
tmp_ans //= 3 # 대괄호가 닫혔으므로 곱했던 3을 다시 나눠줌 | ||
|
||
else: | ||
answer = 0 | ||
|
||
print(answer) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 예외처리를 정리하셔서 이렇게 filter로 걸어주시는게 너무 좋은 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 (8) 이렇게 들어오는 케이스도 필터링이 되나요?