티스토리 뷰
https://mao.snuke.org/tasks/25
Problem 0025 - Balanced
Statement You are given S contains only '(', ')'. Replace it with "yes" if it is correct parenthesis sequence, "no" otherwise. Correct parenthesis sequences are defined as follows. * An empty string is a correct parenthesis sequence. * If string A is a cor
mao.snuke.org
모든 올바른 쌍을 먼저 제거하고 나면, (((( 꼴이거나 ))))) 꼴이거나 ))))((((( 꼴이 된다. 그래서 이 각각의 경우를 하나의 경우로 환원되게 한 후 yes / no를 찍었다.
():
((:(
)):)
)(::no
(::no
)::no
::yes
이렇게 하면 7라인. 각 경우를 하나로 환원하는 경우를 쓸데없이 복잡하게 생각했었는데, 이 모양을 그냥 한 형태가 되게 묶어주면 된다. 올바른 형태를 전부 짝 맞춰서 제거한 다음에는, 모든 ( 를 )로 바꿔주면 항상 한 가지 경우만 있는 것으로 생각할 수 있다. 그래서 이렇게 바꿔준 후 체크하면 5라인으로 줄일 수 있다.
():
(:)
)):)
)::no
::yes
'MAO' 카테고리의 다른 글
0032. Log (0) | 2020.05.28 |
---|---|
0010. Increment (0) | 2020.05.27 |
Markov Algorithm Online (0) | 2020.05.27 |