๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ BOJ #4949

by seolhee2750 2021. 8. 7.
๋ฌธ์ œ ์„ค๋ช…

์„ธ๊ณ„๋Š” ๊ท ํ˜•์ด ์ž˜ ์žกํ˜€์žˆ์–ด์•ผ ํ•œ๋‹ค. ์–‘๊ณผ ์Œ, ๋น›๊ณผ ์–ด๋‘  ๊ทธ๋ฆฌ๊ณ  ์™ผ์ชฝ ๊ด„ํ˜ธ์™€ ์˜ค๋ฅธ์ชฝ ๊ด„ํ˜ธ์ฒ˜๋Ÿผ ๋ง์ด๋‹ค.

์ •๋ฏผ์ด์˜ ์ž„๋ฌด๋Š” ์–ด๋–ค ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ด„ํ˜ธ๋“ค์˜ ๊ท ํ˜•์ด ์ž˜ ๋งž์ถฐ์ ธ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๋Š” ๊ฒƒ์ด๋‹ค.

๋ฌธ์ž์—ด์— ํฌํ•จ๋˜๋Š” ๊ด„ํ˜ธ๋Š” ์†Œ๊ด„ํ˜ธ("()") ์™€ ๋Œ€๊ด„ํ˜ธ("[]")๋กœ 2์ข…๋ฅ˜์ด๊ณ , ๋ฌธ์ž์—ด์ด ๊ท ํ˜•์„ ์ด๋ฃจ๋Š” ์กฐ๊ฑด์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ๋ชจ๋“  ์™ผ์ชฝ ์†Œ๊ด„ํ˜ธ("(")๋Š” ์˜ค๋ฅธ์ชฝ ์†Œ๊ด„ํ˜ธ(")")์™€๋งŒ ์ง์„ ์ด๋ค„์•ผ ํ•œ๋‹ค.
  • ๋ชจ๋“  ์™ผ์ชฝ ๋Œ€๊ด„ํ˜ธ("[")๋Š” ์˜ค๋ฅธ์ชฝ ๋Œ€๊ด„ํ˜ธ("]")์™€๋งŒ ์ง์„ ์ด๋ค„์•ผ ํ•œ๋‹ค.
  • ๋ชจ๋“  ์˜ค๋ฅธ์ชฝ ๊ด„ํ˜ธ๋“ค์€ ์ž์‹ ๊ณผ ์ง์„ ์ด๋ฃฐ ์ˆ˜ ์žˆ๋Š” ์™ผ์ชฝ ๊ด„ํ˜ธ๊ฐ€ ์กด์žฌํ•œ๋‹ค.
  • ๋ชจ๋“  ๊ด„ํ˜ธ๋“ค์˜ ์ง์€ 1:1 ๋งค์นญ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฆ‰, ๊ด„ํ˜ธ ํ•˜๋‚˜๊ฐ€ ๋‘˜ ์ด์ƒ์˜ ๊ด„ํ˜ธ์™€ ์ง์ง€์–ด์ง€์ง€ ์•Š๋Š”๋‹ค.
  • ์ง์„ ์ด๋ฃจ๋Š” ๋‘ ๊ด„ํ˜ธ๊ฐ€ ์žˆ์„ ๋•Œ, ๊ทธ ์‚ฌ์ด์— ์žˆ๋Š” ๋ฌธ์ž์—ด๋„ ๊ท ํ˜•์ด ์žกํ˜€์•ผ ํ•œ๋‹ค.

์ •๋ฏผ์ด๋ฅผ ๋„์™€ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๊ท ํ˜•์žกํžŒ ๋ฌธ์ž์—ด์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋‹จํ•ด๋ณด์ž.

 

์ž…๋ ฅ

ํ•˜๋‚˜ ๋˜๋Š” ์—ฌ๋Ÿฌ์ค„์— ๊ฑธ์ณ์„œ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ๋ฌธ์ž์—ด์€ ์˜๋ฌธ ์•ŒํŒŒ๋ฒณ, ๊ณต๋ฐฑ, ์†Œ๊ด„ํ˜ธ("( )") ๋Œ€๊ด„ํ˜ธ("[ ]")๋“ฑ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 100๊ธ€์ž๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

์ž…๋ ฅ์˜ ์ข…๋ฃŒ์กฐ๊ฑด์œผ๋กœ ๋งจ ๋งˆ์ง€๋ง‰์— ์  ํ•˜๋‚˜(".")๊ฐ€ ๋“ค์–ด์˜จ๋‹ค.

 

์ถœ๋ ฅ

๊ฐ ์ค„๋งˆ๋‹ค ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ๊ท ํ˜•์„ ์ด๋ฃจ๊ณ  ์žˆ์œผ๋ฉด "yes"๋ฅผ, ์•„๋‹ˆ๋ฉด "no"๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ์ œ

์ž…๋ ฅ

So when I die (the [first] I will see in (heaven) is a score list).
[ first in ] ( first out ).
Half Moon tonight (At least it is better than no Moon at all].
A rope may form )( a trail in a maze.
Help( I[m being held prisoner in a fortune cookie factory)].
([ (([( [ ] ) ( ) (( ))] )) ]).
 .
.

์ถœ๋ ฅ

yes
yes
no
no
no
yes
yes

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import Foundation

var input = ""

while true {
    input = readLine()!
    if input == "." { break }
    var stack = [Character]()
    var check = true
    
    for i in input {
        if i == "[" || i == "(" { stack.append(i) }
        else if i == "]" || i == ")" {
            if stack.isEmpty { check = false; break }
            if i == "]" && stack.removeLast() != "[" { check = false; break }
            else if i == ")" && stack.removeLast() != "(" { check = false; break }
        }
    }
    
    if check == false { print("no") }
    else {
        if !stack.isEmpty { print("no") }
        else {print("yes")}
    }
}

์ด ๋ฌธ์ œ๋Š” ๋ฐ”๋กœ ์ด์ „ ํ’€์—ˆ๋˜ #9012 ๊ด„ํ˜ธ ๋ฌธ์ œ์™€ ์œ ์‚ฌํ•˜์—ฌ, ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์ดํ–ˆ๋‹ค.

  • ๋ชจ๋“  ํ’€์ด๋Š” #9012๋ฌธ์ œ์™€ ๋™์ผํ•˜์ง€๋งŒ, ๋”ฑ ํ•œ๊ฐ€์ง€ ์ถ”๊ฐ€๋œ ๋‚ด์šฉ์ด ์žˆ๋‹ค.
  • "["๋Š” "]"์™€๋งŒ ์ง์ด ๋  ์ˆ˜ ์žˆ๊ณ , "("๋Š” ")"์ด๋ž‘๋งŒ ์ง์ด ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
    "]"์ด ์˜ฌ ๊ฒฝ์šฐ์—๋Š” ์Šคํƒ์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๊ฐ€ "["์ธ์ง€ ํ™•์ธ,
    ")"์ด ์˜ฌ ๊ฒฝ์šฐ์—๋Š” ์Šคํƒ ๋งˆ์ง€๋ง‰ ์š”์†Œ๊ฐ€ "("์ธ์ง€ ํ™•์ธํ•ด์ฃผ๋Š” ์กฐ๊ฑด๋ฌธ์„ ๋„ฃ์–ด์คฌ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์•ž์„œ ํ’€์—ˆ๋˜ ๋ฌธ์ œ์™€ ๊ฑฐ์˜ ๊ฐ™์•„ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 


 

๋ฌธ์ œ

https://www.acmicpc.net/problem/4949

๋Œ“๊ธ€