let's get IT with DAVINA ๐Ÿ’ป

[๋ฐฑ์ค€ #4949] ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ ๋ณธ๋ฌธ

DEV_IN/๋ฌธ์ œํ’€์ด

[๋ฐฑ์ค€ #4949] ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 3. 21. 03:23
์Šคํƒ์˜ ํ™œ์šฉ ๋ฌธ์ œ๋กœ, ๊ด„ํ˜ธ์Œ ๋งž์ถ”๊ธฐ ์œ ํ˜•์ด๋‹ค. 

โ“ ๋ฌธ์ œ

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

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

๋ฌธ์ž์—ด์— ํฌํ•จ๋˜๋Š” ๊ด„ํ˜ธ๋Š” ์†Œ๊ด„ํ˜ธ("()") ์™€ ๋Œ€๊ด„ํ˜ธ("[]")๋กœ 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

โ—๏ธ ๋น„๋‹ˆ's ํ’€์ด

  • ์ผ๋‹จ ์ž…๋ ฅ๊ฐ’์„ ๋ฐฐ์—ด๋กœ ๊ฐ ์ค„์„ ๊ฐ€์ ธ์™€์„œ
  • ํ•ด๋‹น ๋ฌธ์ž์—ด ๋ฌธ์žฅ์„ ๋‹ค ๋Œ๋ฆด๋•Œ๋งˆ๋‹ค ( or [ ๋ฅผ ๋งŒ๋‚˜๋ฉด ์Šคํƒ์— ๋„ฃ๊ณ 
  • ) or ] ์„ ๋งŒ๋‚˜๋ฉด ์Šคํƒ์— ์žˆ๋Š”์ง€๋ฅผ ๋น„๊ตํ•ด์•ผ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์œผ๋‚˜
  • ๋ฐ˜๋ณต๋ฌธ๊ณผ ์กฐ๊ฑด๋ฌธ์„ ๊ณ„์† ์‚ฌ์šฉํ•˜๋ฉฐ ๊ทธ ์ˆœ์„œ๊ฐ€ ๋„ˆ๋ฌด ํ—ท๊ฐˆ๋ ค์„œ ๊ณ„์† ์—๋Ÿฌ๊ฐ€ ๋–ด๋‹ค.. ๊ฒฐ๊ตญ์—” ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ฐธ์กฐํ•ด๋ถ€๋ ธ๋‹ค..
const fs = require("fs");
const input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const res = [];

for (let el of input) { //ํ•˜๋‚˜์˜ ์ž…๋ ฅ๋งˆ๋‹ค ๋‹ค ๋Œ๊ธฐ
    let compare = []; //์Šคํƒ
    let isMatch = true;
    if (el === ".") break;
    for (let i = 0; i < el.length; i++) {
        if (el[i] === "[" || el[i] === "(") { //๋ฌธ์ž์—ด์— [๋‚˜ (๊ฐ€ ์žˆ๋Š”์ง€ ์‚ดํ”ผ๊ณ 
            compare.push(el[i]); //์žˆ์œผ๋ฉด ์Šคํƒ์— ์ถ”๊ฐ€
        } else if (el[i] === "]") { //]๋ฅผ ๋งŒ๋‚ฌ์„ ๊ฒฝ์šฐ ๋จผ์ € ๋ณด๊ธฐ
            if (compare[compare.length - 1] === "[") { //์Šคํƒ ๋งˆ์ง€๋ง‰์— [๊ฐ€ ์žˆ์–ด?
                compare.pop(); //๊ทธ๋Ÿผ ์ง ๋งž์•˜์œผ๋‹ˆ ์—†์• 
            } else { //์Šคํƒ์— [๊ฐ€ ์—†์–ด?
                isMatch = false; //์•ˆ๋งž์•„์š”
                break;
            }
        } else if (el[i] === ")") { //)๊ฐ€ ๋‚˜์™”์„๋•?
            if (compare[compare.length - 1] === "(") { //(๊ฐ€ ์žˆ์–ด?
                compare.pop(); //์ง๋งž๋Š”๋‹ค ์—†์• 
            } else { //์Šคํƒ์— (๊ฐ€ ์—†์–ด?
                isMatch = false; //์•ˆ๋งž์•„์š”
                break;
            }
        } else if (el[i] === ".") break;
    }
    if (compare.length > 0 || isMatch === false) {
        res.push("no");
    } else res.push("yes");
}

console.log(res.join("\n"));
Comments