We will find a way, we always have.

-interstellar

Problem Solving/λ°±μ€€

[λ°±μ€€] 1918번: ν›„μœ„ν‘œκΈ°μ‹ - 파이썬

Redddy 2022. 5. 16. 22:29

πŸ˜€ μ„œλ‘ 

사싀 μ €λ²ˆμ£Όμ— ν›„μœ„ν‘œκΈ°μ‹ κ΄€λ ¨ 문제λ₯Ό ν’€μ—ˆμ—ˆλ‹€. μš”κΈ°!
κ°œλ…μ€ μ΄ν•΄ν–ˆμœΌλ‹ˆ λ‚ λ‘œλ¨Ήμ„ μƒκ°μœΌλ‘œ κ³¨λ“œ 3에 λ„μ „ν–ˆλ‹€κ°€ λ³΄κΈ°μ’‹κ²Œ μ‹€νŒ¨ν–ˆλ‹€γ…Ž.γ…Ž

μœ„μ˜ λ¬Έμ œν•˜κ³  이번 문제의 차이점은 μˆœμ„œμ΄λ‹€. ν›„μœ„ ν‘œκΈ°μ‹2번 λ¬Έμ œλŠ” ν›„μœ„ν‘œκΈ°μ‹μ„ μ€‘μœ„ν‘œκΈ°μ‹μœΌλ‘œ λ°”κΎΈμ–΄ 값을 κ³„μ‚°ν•˜λŠ” κ±°μ˜€λ‹€λ©΄, 이번 λ¬Έμ œλŠ” μ€‘μœ„ν‘œκΈ°μ‹μ„ ν›„μœ„ν‘œκΈ°μ‹μœΌλ‘œ λ°”κΎΈκΈ°λ§Œ ν•˜λ©΄ λ˜λŠ” κ²ƒμ΄μ—ˆλ‹€!

μ€‘μœ„ν‘œκΈ°μ‹μ„ ν›„μœ„ν‘œκΈ°μ‹μœΌλ‘œ λ³€κ²½
ν›„μœ„ν‘œκΈ°μ‹μ„ μ€‘μœ„ν‘œκΈ°μ‹μœΌλ‘œ λ°”κΎΈκ³  값을 좜λ ₯

μ΄λ ‡κ²Œ 봀을 λ•Œ ν›„μžκ°€ 더 μ–΄λ €μšΈ 것 κ°™μ§€λ§Œ,, μ€‘μœ„ν‘œκΈ°μ‹μ„ ν›„μœ„ν‘œκΈ°μ‹μœΌλ‘œ λ°”κΎΈλ €λ©΄ κ΄„ν˜ΈμΉ˜λŠ” μž‘μ—…μ„ ν•΄μ•Όν•œλ‹€.
이게 λ‚œκ΄€μ΄μ—ˆλ‹€...

 

 

 

[λ°±μ€€] 1935번: ν›„μœ„ ν‘œκΈ°μ‹2 - 파이썬

πŸ“• 문제 풀이 ν›„μœ„ ν‘œκΈ°μ‹μœΌλ‘œ λ¬Έμžκ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ 연산을 ν•˜μ—¬ 값을 좜λ ₯ν•œλ‹€. ν›„μœ„ν‘œκΈ°μ‹μ΄λž€? ν›„μœ„ν‘œκΈ°μ‹μ€ 컴퓨터가 μ—°μ‚°ν•  λ•Œ μ‚¬μš©ν•˜λŠ” 방법이닀. μš°λ¦¬κ°€ μ—°μ‚°ν•  λ•Œ μ‚¬μš©ν•˜λŠ” (3*4+6)/2 이런

lazypazy.tistory.com

 

πŸ“• 문제 풀이

1. κ³±ν•˜κΈ°μ™€ λ‚˜λˆ„κΈ°κ°€ μš°μ„ μˆœμœ„κ°€ λ†’μœΌλ‹ˆ κ΄„ν˜Έλ₯Ό μ”Œμ–΄μ€€λ‹€.
2. λ”ν•˜κΈ°μ™€ 뺴기에 κ΄„ν˜Έλ₯Ό μ”Œμ–΄μ€€λ‹€.
3. μ€‘μœ„ ν‘œκΈ°μ‹μ„ ν›„μœ„ ν‘œκΈ°μ‹μœΌλ‘œ λ°”κΏ”μ€€λ‹€. μžμ„Έν•œ λ‚΄μš©μ€ μœ„μ— κΈ€ μ°Έμ‘°..

 

πŸ’» μ‹€νŒ¨μ½”λ“œ!

sick = list(input())
#print(sick)
stk = []
first = {"*", "/"}
second = {"+","-"}
gualho = []
backtrack = [] # λ’€λ‘œ κ°ˆλ•Œ κ΄„ν˜Έ 체크
some = []
result = []
check = False
check2 = False

c1 = sick.count("+") + sick.count("-") + sick.count("*") + sick.count("/")
c2 = sick.count("(")

if c1 != c2:
    for i in sick:
        if i == "(":
            gualho.append(i)
        elif i == ")":
            gualho.pop()

        if i in first:
            a = stk.pop()
            if a == ")":
                backtrack.append(a)
                some.append(i)
                some.append(a)
                while backtrack:
                    b = stk.pop()
                    some.append(b)
                    if b == ")":
                        backtrack.append(b)
                    elif b == "(":
                        backtrack.pop()
                some.append("(")
                some = some[::-1]
                stk.extend(some)
                some = []
                check2 = True

            else:
                stk.append("(")
                stk.append(a)
                stk.append(i)
                check = True
        elif check2 == True and i == ")" and len(gualho) == 0:
            stk.append(i)
            stk.append(")")
            check2 = False
        elif check2 == True and 65 <= ord(i) <= 90 and len(gualho) == 0:
            stk.append(i)
            stk.append(")")
            check2 = False
        elif i not in first and check == True and len(gualho) == 0:
            stk.append(i)
            stk.append(")")
            check = False
        elif i not in first and check == True and len(gualho) != 0:
            stk.append(i)
        elif i not in first and check == False:
            stk.append(i)

    result = stk.copy()
else:
    result = sick

gualo_cnt = stk.count("(")
yensan_cnt = stk.count("+") + stk.count("-") + stk.count("*") + stk.count("/")

sick = stk.copy()
stk = []

if gualo_cnt != yensan_cnt:
    for i in sick:
        if i == "(":
            gualho.append(i)
        elif i == ")":
            gualho.pop()

        if i in second:
            a = stk.pop()
            if a == ")":
                backtrack.append(a)
                some.append(i)
                some.append(a)
                while backtrack:
                    b = stk.pop()
                    some.append(b)
                    if b == ")":
                        backtrack.append(b)
                    elif b == "(":
                        backtrack.pop()
                some.append("(")
                some = some[::-1]
                stk.extend(some)
                some = []
                check2 = True
            else:
                stk.append("(")
                stk.append(a)
                stk.append(i)
                check = True
        elif i in first:
            stk.append(i)
        elif check2 == True and i == ")" and len(gualho) == 0:
            stk.append(i)
            stk.append(")")
            check2 = False
        elif check2 == True and 65 <= ord(i) <= 90 and len(gualho) == 0:
            stk.append(i)
            stk.append(")")
            check2 = False
        elif i not in first and check == True and len(gualho) == 0:
            stk.append(i)
            stk.append(")")
            check = False
        elif i not in first and check == True and len(gualho) != 0:
            stk.append(i)
        elif i not in first and check == False:
            stk.append(i)
    result = stk

else:
    pass

print(*result, sep="")

# 이제 ν›„μœ„ν‘œκΈ°μ‹μœΌλ‘œ λ³€ν™˜
tmp = []
for k in result:
    try:
        if k == "(":
            pass
        elif 65 <= ord(k) <= 90:
            print(k, end="")
        elif k == ")":
            print(tmp.pop(), end="")
        else:
            tmp.append(k)
    except IndexError:
        pass
if len(tmp) != 0:
    print(*tmp)

 

μ½”λ“œμ˜ 문제점

μ€‘λ³΅κ΄„ν˜Έ input : (A+B)*C, output : (((A+B))*C)

κ΄„ν˜Έκ°€ ν•˜λ‚˜λ§Œ μžˆμ–΄λ„ λ˜λŠ”λ° κ΄„ν˜Έκ°€ μžˆλŠ”μ±„λ‘œ μž…λ ₯ν•΄μ˜¨λ‹€λ©΄ κ΄„ν˜Έκ°€ μ€‘λ³΅ν•΄μ„œ λ“€μ–΄κ°„λ‹€...

 

 

κ΄„ν˜Έκ°€ λ‹€ μ•ˆλ‹Ίνž˜ input : A+B*C*((D-E)*G) =>  (A+(((B*C)*(((D-E)*G)) 

λ³΄μ‹œλ©΄ μ™Όκ΄„ν˜Έλž‘ μš°κ΄„ν˜Έ κ°―μˆ˜κ°€ λ§žμ§€ μ•Šλ‹€...

 

μ΄λ ‡κ²Œ κ΄„ν˜Έ μ”Œμš°λŠ”λ°λΆ€ν„° λ¬Έμ œκ°€ 생겼고, μ½”λ“œκ°€ λ„ˆλ¬΄ κΈΈμ–΄μ Έ λ‚˜λ„ λ‚΄ μ½”λ“œκ°€ 뭔지 λͺ¨λ₯΄κ²Œ λ˜μ—ˆλ‹€!

 

흑ㅠ

πŸ“Ž 문제 링크 : ν›„μœ„ ν‘œκΈ°μ‹

 

1918번: ν›„μœ„ ν‘œκΈ°μ‹

첫째 쀄에 μ€‘μœ„ ν‘œκΈ°μ‹μ΄ 주어진닀. 단 이 μˆ˜μ‹μ˜ ν”Όμ—°μ‚°μžλŠ” μ•ŒνŒŒλ²³ λŒ€λ¬Έμžλ‘œ 이루어지며 μˆ˜μ‹μ—μ„œ ν•œ λ²ˆμ”©λ§Œ λ“±μž₯ν•œλ‹€. 그리고 -A+B와 같이 -κ°€ κ°€μž₯ μ•žμ— μ˜€κ±°λ‚˜ AB와 같이 *κ°€ μƒλž΅λ˜λŠ” λ“±μ˜

www.acmicpc.net