Conversation
|
내가 푼 연산 방법이랑 로직은 비슷해보이지만 난 스택으로 풀지 못해서 자세히 읽어보니 생각을 조금만 바꾸면 풀 수 있었을 듯한 문제였던거 같다. 풀이에 주석도 잘 들어가 있어 따라가기 쉬웠고 깔끔한 풀이었던거 같다! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
안정적인 문자열 (BOJ 4889)
주어진 문자열은
{와}로 구성되어 있으며, 이를 안정적인 문자열로 만들기 위해 필요한 최소 연산 횟수를 계산하는 문제이다.문자열이
-로 시작하면 입력이 종료된다.문제 개요
안정적인 문자열이란 다음 조건을 만족한다.
{와}로만 이루어져 있다.{는 올바르게 매칭되는}를 가져야 한다.문자열을 안정적으로 만들기 위해 다음 연산을 사용할 수 있다.
{→}}→{이 두 연산 중 필요한 최소 횟수를 계산한다.
💡 해결 방법
스택을 사용하여 문자열을 왼쪽부터 순서대로 처리한다.
1. 문자 처리 규칙
{→ 스택에 push}→→
}를{로 바꿔야 하므로 연산 +1→ 바꾼
{를 스택에 push→ 정상 매칭이므로 pop
2. 처리 후 스택 분석
모든 문자를 처리한 후 스택에는 매칭되지 않은
{들만 남는다.예:
"{{{{"→ 4개 남음두 개씩 짝지어
"{{"→"{ }"로 바꾸는 데 1회 필요하므로,만큼 연산을 더해준다.
총 연산 횟수