Notice
Recent Posts
Recent Comments
Link
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[๋ฐฑ์ค€ 4949๋ฒˆ] ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Baekjoon

[๋ฐฑ์ค€ 4949๋ฒˆ] ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs)

Lennon 2022. 1. 20. 19:44
728x90
๋ฐ˜์‘ํ˜•

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

 

4949๋ฒˆ: ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ

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

www.acmicpc.net

const fs = require('fs');
let input = fs.readFileSync('../input.txt').toString().trim().split('\n');
input.pop();

const answer = [];

input.filter((v) => {
  const bracketArr = [];

  for (let i = 0; i < v.length; i++) {
    switch (v[i]) {
      case '[':
        bracketArr.push('[');
        break;

      case ']':
        if (bracketArr.includes('[')) {
          let index = bracketArr.lastIndexOf('[');
          if (bracketArr[bracketArr.length - 1] !== '[') {
            bracketArr.push(']');
            break;
          }
          bracketArr.splice(index, 1);
        } else {
          bracketArr.push(']');
        }
        break;

      case '(':
        bracketArr.push('(');
        break;

      case ')':
        if (bracketArr.includes('(')) {
          let index = bracketArr.lastIndexOf('(');
          if (bracketArr[bracketArr.length - 1] !== '(') {
            bracketArr.push(')');
            break;
          }
          bracketArr.splice(index, 1);
        } else {
          bracketArr.push(')');
        }
        break;
    }
  }

  if (bracketArr.length === 0) {
    answer.push('yes');
  } else {
    answer.push('no');
  }
});

console.log(answer.join('\n'));

bracketArr => switch๋ฌธ์„ ์ด์šฉํ•ด [ , ] , ( , ) ๋งŒ ๋„ฃ๋Š” ๋ฐฐ์—ด์ด๋‹ค. 

 

[ , ( ์€ ๋ฌด์กฐ๊ฑด ๋ฐฐ์—ด์— pushํ•˜๋ฉฐ,  ๋‹ซ๋Š” ๊ด„ํ˜ธ์ผ ๋•Œ๋งŒ ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•œ๋‹ค.

 

19๋ฒˆ์งธ ์ค„์—์„œ, ๋งŒ์•ฝ bracketArr์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์˜ ๊ฐ’์ด [ ์ด ์•„๋‹ˆ๋ผ๋ฉด ๊ด„ํ˜ธ๊ฐ€ ์„ฑ๋ฆฝ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ทธ๋ƒฅ [ ๊ฐ’์„ ๋‹ค์‹œ push ํ•œ๋‹ค.

์‰ฝ๊ฒŒ ์˜ˆ๋ฅผ ๋“ค์–ด ์„ค๋ช…ํ•˜๋ฉด

 

input๊ฐ’์ด  [์•ˆ๋…•ํ•˜(์„ธ์š”]์ €๋Š” ๋ˆ„๊ตฌ์ž…๋‹ˆ๋‹ค). ๋ผ๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.

input๊ฐ’์„ ์ˆœํšŒํ•˜๋ฉฐ bracketArr์—๋Š” ์ˆœ์„œ๋Œ€๋กœ [, ( ๊ฐ€ ์‚ฝ์ž…๋  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‹ค ] ๋ฅผ ๋งŒ๋‚˜๋ฉด bracketArr์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ํ™•์ธํ•œ๋‹ค.

๋งˆ์ง€๋ง‰ ๊ฐ’์ด ( ์ด๋ฉฐ, ๊ด„ํ˜ธ๊ฐ€ ์„ฑ๋ฆฝ์ด ์•ˆ๋˜๋ฏ€๋กœ ๊ทธ๋ƒฅ ] ๋ฅผ push ํ•œ๋‹ค. 

๊ทธ๋Ÿฌ๋ฉด bracketArr = [ '[', '(', ']' ] ๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ๊ทธ๋‹ค์Œ ) ๋ฅผ ๋งŒ๋‚˜๋ฉด ๋งˆ์ง€๋ง‰ ๊ฐ’์ด ( ์ด ์•„๋‹ˆ๋ฏ€๋กœ ๋‹ค์‹œ ) ๊ฐ’์„ push ํ•œ๋‹ค.

 

๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚ฌ์„ ๋•Œ bracketArr  

bracketArr = [ ]

bracketArr = [ '[' ]

bracketArr = [ '[', '(' ]

bracketArr = [ '[', '(', ']' ]       // ์กฐ๊ฑด ๊ฒ€์‚ฌ ๊ด„ํ˜ธ๊ฐ€ ์„ฑ๋ฆฝ ์•ˆ๋˜๋ฏ€๋กœ push

bracketArr = [ '[', '(', ']', ')'] // ์กฐ๊ฑด ๊ฒ€์‚ฌ ๊ด„ํ˜ธ๊ฐ€ ์„ฑ๋ฆฝ ์•ˆ๋˜๋ฏ€๋กœ push

 

๊ด„ํ˜ธ๊ฐ€ ์ œ๋Œ€๋กœ ์„ฑ๋ฆฝ์ด ๋˜๋ฉด bracketArr์˜ ๊ฒฐ๊ณผ๋Š” ๋นˆ ๋ฐฐ์—ด์ด๋‹ค. 48๋ฒˆ ์ค„์—์„œ ์กฐ๊ฑด์„ ํ™•์ธํ•˜๊ณ  answer๋ฐฐ์—ด์— yes, no ๊ฐ’์„ ๋„ฃ๊ณ  

console์„ ๋„์šฐ๋ฉด ์ •๋‹ต์ด๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
Comments