ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค [lv2] ๊ด„ํ˜ธ๋ณ€ํ™˜ ํŒŒ์ด์ฌ

2021. 4. 25. 15:29ใ†๐Ÿ”ฑ Algorithm/Else

์ ‘๊ทผ

๋ณต์žกํ•ด ๋ณด์ด์ง€๋งŒ, ๋ฌธ์ œ์˜ ์š”๊ตฌ์กฐ๊ฑด์„ ์ฐจ๋ก€๋กœ ๊ตฌํ˜„ ํ•˜๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.. 

๋‹จ, ํ•˜๋‚˜์˜ ํ•จ์ˆ˜์— ๋ชจ๋“  ๊ฒƒ์„ ํ•ด๊ฒฐํ•˜๋ คํ•˜๋ฉด ๋ณต์žกํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— 3๊ฐœ์˜ ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

 

 

๋ฌธ์ œ ์š”๊ตฌ ์‚ฌํ•ญ

1. ์ž…๋ ฅ์ด ๋นˆ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ, ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

2. ๋ฌธ์ž์—ด w๋ฅผ ๋‘ "๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด" u, v๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, u๋Š” "๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด"๋กœ ๋” ์ด์ƒ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์—†์–ด์•ผ ํ•˜๋ฉฐ, v๋Š” ๋นˆ ๋ฌธ์ž์—ด์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

3. ๋ฌธ์ž์—ด u๊ฐ€ "์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด" ์ด๋ผ๋ฉด ๋ฌธ์ž์—ด v์— ๋Œ€ํ•ด 1๋‹จ๊ณ„๋ถ€ํ„ฐ ๋‹ค์‹œ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

   3-1. ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ u์— ์ด์–ด ๋ถ™์ธ ํ›„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

4. ๋ฌธ์ž์—ด u๊ฐ€ "์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด"์ด ์•„๋‹ˆ๋ผ๋ฉด ์•„๋ž˜ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

   4-1. ๋นˆ ๋ฌธ์ž์—ด์— ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋กœ '('๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค.

   4-2. ๋ฌธ์ž์—ด v์— ๋Œ€ํ•ด 1๋‹จ๊ณ„๋ถ€ํ„ฐ ์žฌ๊ท€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ ์ด์–ด ๋ถ™์ž…๋‹ˆ๋‹ค.

   4-3. ')'๋ฅผ ๋‹ค์‹œ ๋ถ™์ž…๋‹ˆ๋‹ค.

   4-4. u์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋‚˜๋จธ์ง€ ๋ฌธ์ž์—ด์˜ ๊ด„ํ˜ธ ๋ฐฉํ–ฅ์„ ๋’ค์ง‘์–ด์„œ ๋’ค์— ๋ถ™์ž…๋‹ˆ๋‹ค.

   4-5. ์ƒ์„ฑ๋œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„

 

1. ์ž…๋ ฅ์ด ๋นˆ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ, ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

def solution(p):
    return p if correct(p) else recursive(p)
    
def recursive(sentence):
    if sentence == '':
        return '' # ์ž…๋ ฅ ๋ฌธ์ž์—ด์ด ๊ณต๋ฐฑ์ด๋ฉด, ๊ณต๋ฐฑ return
    u, v = detatch(sentence)
    if correct(u):
        return u + recursive(v)
    else:
        return '(' + recursive(v) + ')' + ''.join(list(map(lambda x: ")" if x =="(" else "(", u[1:-1])))

 

2. ๋ฌธ์ž์—ด w๋ฅผ ๋‘ "๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด" u, v๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, u๋Š” "๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด"๋กœ ๋” ์ด์ƒ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์—†์–ด์•ผ ํ•˜๋ฉฐ, v๋Š” ๋นˆ ๋ฌธ์ž์—ด์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

- ๋ถ„๋ฆฌํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์šฐ์„  queue๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

- u๊ฐ€ ์šฐ์„  ๊ท ํ˜•์žกํžŒ ๋ฌธ์ž์—ด์˜ ํ˜•ํƒœ๋ฅผ ๋„๋ฉด ๊ณง ๋ฐ”๋กœ return ์‹œ์ผœ u, v๋ฅผ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

def detatch(sentence):
    temp_que = deque(sentence)
    left, right = 0, 0
    u, v = '', ''

    while temp_que:
        u += temp_que.popleft()
        if u[-1] == "(":
            left += 1
        else:
            right += 1

        if left == right:
            break

    v = ''.join(list(temp_que))
    return u, v

 

3. ๋ฌธ์ž์—ด u๊ฐ€ "์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด" ์ด๋ผ๋ฉด ๋ฌธ์ž์—ด v์— ๋Œ€ํ•ด 1๋‹จ๊ณ„๋ถ€ํ„ฐ ๋‹ค์‹œ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

   3-1. ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ u์— ์ด์–ด ๋ถ™์ธ ํ›„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

- correct() ํ•จ์ˆ˜์—์„œ True๊ฐ€ ๋ฆฌํ„ด๋  ๋•Œ์™€ ๊ทธ๋ ‡์ง€ ์•Š์„ ๋•Œ๋ฅผ ๋ถ„๊ธฐ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

- ๋ฌธ์ž์—ด u๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด ์ผ ๋•Œ, v ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด ๋‹ค์‹œ recursive() ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋„ฃ์–ด ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. (์žฌ๊ท€ ๊ตฌ์กฐ)

def correct(sentence):
    stack = []
    for sen in sentence:
        if sen == "(":
            stack.append(sen)
        else:
            if len(stack) == 0:
                return False
            else:
                stack.pop()

    return True if len(stack) == 0 else False
    
def recursive(sentence):
    if sentence == '':
        return ''
    u, v = detach(sentence)
    if correct(u):
        return u + recursive(v)
    else:
        # return '(' + recursive(v) + ')' + reverse(u[1:-1])
        return '(' + recursive(v) + ')' + ''.join(list(map(lambda x: ")" if x =="(" else "(", u[1:-1])))

 

 

4. ๋ฌธ์ž์—ด u๊ฐ€ "์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด"์ด ์•„๋‹ˆ๋ผ๋ฉด ์•„๋ž˜ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

   4-1. ๋นˆ ๋ฌธ์ž์—ด์— ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋กœ '('๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค.

   4-2. ๋ฌธ์ž์—ด v์— ๋Œ€ํ•ด 1๋‹จ๊ณ„๋ถ€ํ„ฐ ์žฌ๊ท€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ ์ด์–ด ๋ถ™์ž…๋‹ˆ๋‹ค.

   4-3. ')'๋ฅผ ๋‹ค์‹œ ๋ถ™์ž…๋‹ˆ๋‹ค.

   4-4. u์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋‚˜๋จธ์ง€ ๋ฌธ์ž์—ด์˜ ๊ด„ํ˜ธ ๋ฐฉํ–ฅ์„ ๋’ค์ง‘์–ด์„œ ๋’ค์— ๋ถ™์ž…๋‹ˆ๋‹ค.

   4-5. ์ƒ์„ฑ๋œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

- ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ else ์ดํ•˜์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

- 4-4 ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ฃผ์˜ํ•ด์•ผํ•˜๋Š”๋ฐ, u[1:-1] ๋ฌธ์ž์—ด์„ ๊ทธ์ € ๋’ค์ง‘๋Š” ๊ฒƒ์ด ์•„๋‹Œ, u[1:-1]์˜ ์›์†Œ ํ•˜๋‚˜ํ•˜๋‚˜์˜ ๊ด„ํ˜ธ๋ฅผ ๋’ค์ง‘์–ด ์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.

- ์ด ์‚ฌํ•ญ์€ ๋žŒ๋‹ค์‹์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

def recursive(sentence):
    if sentence == '':
        return ''
    u, v = detach(sentence)
    if correct(u):
        return u + recursive(v)
    else:
        return '(' + recursive(v) + ')' + ''.join(list(map(lambda x: ")" if x =="(" else "(", u[1:-1])))

 

์ „์ฒด ์ฝ”๋“œ

from collections import deque


def detatch(sentence):
    temp_que = deque(sentence)
    left, right = 0, 0
    u, v = '', ''

    while temp_que:
        u += temp_que.popleft()
        if u[-1] == "(":
            left += 1
        else:
            right += 1

        if left == right:
            break

    v = ''.join(list(temp_que))
    return u, v


def recursive(sentence):
    if sentence == '':
        return ''
    u, v = detatch(sentence)
    if correct(u):
        return u + recursive(v)
    else:
        return '(' + recursive(v) + ')' + ''.join(list(map(lambda x: ")" if x =="(" else "(", u[1:-1])))


def correct(sentence):
    stack = []
    for sen in sentence:
        if sen == "(":
            stack.append(sen)
        else:
            if len(stack) == 0:
                return False
            else:
                stack.pop()

    return True if len(stack) == 0 else False


def solution(p):
    return p if correct(p) else recursive(p)