๐Ÿ”ฅ Algorithm/Baekjoon

[๋ฐฑ์ค€ 2607๋ฒˆ] ๋น„์Šทํ•œ ๋‹จ์–ด - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs)

Lennon 2022. 5. 7. 00:27
728x90
๋ฐ˜์‘ํ˜•

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

 

2607๋ฒˆ: ๋น„์Šทํ•œ ๋‹จ์–ด

์ฒซ์งธ ์ค„์—๋Š” ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ๋Š” ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ชจ๋“  ๋‹จ์–ด๋Š” ์˜๋ฌธ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋Š” 100๊ฐœ ์ดํ•˜์ด๋ฉฐ, ๊ฐ ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 10 ์ด

www.acmicpc.net

const { off } = require('process');
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let input = [];
rl.on('line', function (line) {
  input.push(line);
  if (input.length === +input[0] + 1) {
    rl.close();
  }
}).on('close', function () {
  input.shift();

  const firstStr = input[0];
  const validationStr = input.slice(1);

  console.log(
    solution(
      firstStr.split(''),
      validationStr.map((v) => v.split(''))
    )
  );
});

function solution(standard, validation) {
  let answer = 0;

  validation.forEach((v) => {
    const start = [...standard];

    if (v.length < start.length) {
      for (let i = 0; i < v.length; i++) {
        if (start.includes(v[i])) {
          const idx = start.indexOf(v[i]);
          start.splice(idx, 1);
        }
      }
      if (start.length === 1) {
        answer++;
      }
    } else {
      for (let i = 0; i < start.length; i++) {
        if (v.includes(start[i])) {
          const idx = v.indexOf(start[i]);
          v.splice(idx, 1);
        }
      }
      if (v.length === 1 || v.length === 0) {
        answer++;
      }
    }
  });

  return answer;
}

์‹ค๋ฒ„4 ์ฃผ์ œ์— ์ •๋‹ต๋ฅ ์ด 27ํผ์ธ ๋ฌธ์ œ์ด๋‹ค.

๋ณธ์ธ๋„ ๊ณ„์† ํ‹€๋ ค์„œ ์˜๋ฌธ์„ ๊ฐ€์ง€๊ณ  ์ฝ”๋“œ๋ฅผ ํ•œ์ค„ํ•œ์ค„ ํ•ด์„ํ•˜๋‹ค๋ณด๋‹ˆ ๋ฐ”๋ณด๊ฐ™์ด ์ฒ˜๋ฆฌํ•œ ๋ถ€๋ถ„์ด ์žˆ์–ด์„œ ๊ทธ๋Ÿฐ ๊ฑฐ์˜€๋‹ค.

๋น„๊ตํ•  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋”ฐ์ ธ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ํ•ด์„ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค. 

728x90
๋ฐ˜์‘ํ˜•