Notice
Recent Posts
Recent Comments
Link
ยซ   2024/06   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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

[๋ฐฑ์ค€ 1062๋ฒˆ] ๊ฐ€๋ฅด์นจ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Baekjoon

[๋ฐฑ์ค€ 1062๋ฒˆ] ๊ฐ€๋ฅด์นจ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs)

Lennon 2022. 2. 11. 19:34
728x90
๋ฐ˜์‘ํ˜•

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

 

1062๋ฒˆ: ๊ฐ€๋ฅด์นจ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ N๊ณผ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. N์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , K๋Š” 26๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜ ๋˜๋Š” 0์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๋‚จ๊ทน ์–ธ์–ด์˜ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด๋Š” ์˜์–ด ์†Œ๋ฌธ

www.acmicpc.net

const fs = require('fs');
let input = fs.readFileSync('../input.txt').toString().trim().split('\n');
const [N, K] = input.shift().split(' ').map(Number);
const words = ['a', 'n', 't', 'i', 'c'];
input = input.map((v) => v.slice(4, v.length - 4));

const combinations = function* (elements, selectNumber) {
  for (let i = 0; i < elements.length; i++) {
    if (selectNumber === 1) {
      yield [elements[i]];
    } else {
      const fixed = elements[i];
      const rest = elements.slice(i + 1);

      for (const a of combinations(rest, selectNumber - 1)) {
        yield [fixed, ...a];
      }
    }
  }
};

const filterWords = (arr) => {
  return arr.map((v) => {
    const word = [...new Set(v.split(''))].filter((ele) => !words.includes(ele));
    return word;
  });
};

if (K < 5) {
  console.log(0);
} else if (K === 5) {
  const filterWord = filterWords(input);
  console.log(filterWord.filter((v) => v.length === 0).length);
} else {
  const filterWord = filterWords(input);
  const flatWord = [...new Set(filterWord.flat())];

  let answer = 0;

  if (K > [...flatWord, ...words].length) {
    console.log(N);
    return;
  }

  for (let ele of combinations(flatWord, K - 5)) {
    const str = [...words, ...ele];
    let count = 0;
    Loop: for (let i = 0; i < input.length; i++) {
      for (let j = 0; j < input[i].length; j++) {
        if (!str.includes(input[i][j])) {
          continue Loop;
        }
      }
      count++;
    }
    answer = Math.max(answer, count);
  }

  console.log(answer);
}

์‰ฌ์šด ๋ฌธ์ œ์ด์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ํ’€๋ฉด ๊ณ„์† ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ๋ญ๊ฐ€ ์žˆ์„๊นŒ ํ•˜๋‹ค๊ฐ€ 

https://kscodebase.tistory.com/520

 

[node.js] ๊ฐ€๋ฅด์นจ ( ๋ฐฑ์ค€ 1062๋ฒˆ )

// ๋ฐฑ์ค€ 11723๋ฒˆ node.js๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์œผ๋กœ ํ’€ ์ˆ˜ ์—†๋Š” ๋ฌธ์ œ. const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const input =..

kscodebase.tistory.com

์ด ๋ถ„์˜ ๋ธ”๋กœ๊ทธ์— ์กฐํ•ฉ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์ผ๋”๋‹ˆ ๋ฐ”๋กœ ํ†ต๊ณผ๊ฐ€ ๋๋‹ค. ์ฝ”๋“œ๋ฅผ ์„ค๋ช…ํ•˜์ž๋ฉด

 

Function combinations

์กฐํ•ฉ์ฝ”๋“œ์ด๋ฉฐ, ์กฐํ•ฉ ๊ฒฐ๊ณผ๊ฐ€ objํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค.

Function filterWords

words['a', 'n', 't', 'i', 'c'] ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋‚จ๋Š” ๋ฌธ์ž๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.

ex) [ 'antihj' ] => ['hj']

 

 

1. ์ฃผ์–ด์ง„ input์—์„œ ์ค‘๋ณต๋˜๋Š” ๊ฐ’์ธ ์–‘๋ 4๊ฐœ๋ฅผ ์ž˜๋ผ์„œ ๋ฐฐ์—ดํ˜•ํƒœ๋กœ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

ex)  [ 'antarctica', 'antahellotica', 'antacartica' ] => [ 'rc', 'hello', 'car' ]

 

2. ๊ธฐ๋ณธ์ ์œผ๋กœ K๊ฐ€ 5๊ฐœ ์ด์ƒ์ด์–ด์•ผ ๋งŒ์กฑํ•˜๋Š” ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ตœ์†Œ 0๊ฐœ๋ผ๋„ ๋˜๋ฏ€๋กœ K๊ฐ€ 5๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ทธ๋ƒฅ 0์„ ์ฝ˜์†”๋กœ ์ถœ๋ ฅํ•ด์ค€๋‹ค.

 

3. K๊ฐ€ 5๊ฐœ๋ฉด filterWordsํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด words์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ์•Š๋Š” ๋ฌธ์ž๋งŒ ํ•„ํ„ฐ๋งํ•ด์ฃผ๊ณ , ๊ธธ์ด๊ฐ€ 0์ธ input๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฝ˜์†”๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

=> ๊ธธ์ด๊ฐ€ 0์ด๋ผ๋Š” ๊ฑด ๋ชจ๋‘ words์•ˆ์— ํฌํ•จ๋˜์–ด์žˆ๋Š” ๋ฌธ์ž์ด๋ฏ€๋กœ ๋ฐฐ์šธ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์ด๋‹ค.

ex) [ 'c', 'hello', 'car' ] => [ [], ['h', 'e', 'l', 'o'], ['r']] => ์ •๋‹ต: 1๊ฐœ 

 

4. K๊ฐ€ 5 ์ดˆ๊ณผ์ด๋ฉด ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ filterWordsํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ž๋งŒ ํ•„ํ„ฐ๋งํ•ด์ฃผ๊ณ , ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ์ค‘๋ณต๋˜์ง€ ์•Š๋„๋ก ์ƒˆ๋กœ์šด ๋ฐฐ์—ด flatWord์— ์ดˆ๊ธฐํ™”์‹œ์ผœ์ค€๋‹ค. 

ex) [ 'rc', 'hello', 'car' ]  => [ [ 'r' ], [ 'h', 'e', 'l', 'o' ], [ 'r' ] ]  => [ 'r', 'h', 'e', 'l', 'o' ]

 

5. ๋งŒ์•ฝ flatWord์™€ words๋ฅผ ํ•ฉ์นœ ๊ธธ์ด๊ฐ€ K๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ๋ชจ๋“  ์–ธ์–ด๋ฅผ ๋ฐฐ์šธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ ์ž…๋ ฅ๊ฐ’ N์„ ์ฝ˜์†”๋กœ ์ฐ์–ด์ค€๋‹ค.

ex) [ 'r', 'h', 'e', 'l', 'o', ...words ].length < K    ๋ชจ๋“  ์–ธ์–ด๋ฅผ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์Œ!

 

6. ์•„๋‹ˆ๋ผ๋ฉด ์กฐํ•ฉ ๊ฒฐ๊ณผ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ input๊ฐ’์—์„œ ๋งŒ์กฑํ•˜๋Š” ์–ธ์–ด์˜ ๊ฐœ์ˆ˜์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•ด์ฃผ๊ณ  ์ฝ˜์†”๋กœ ์ฐ์–ด์ค€๋‹ค.

 

 

์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ K๊ฐ€ 5์ผ๋•Œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ƒ ๋งˆ๋ƒ์— ๋‹ฌ๋ฆฐ ๊ฒƒ ๊ฐ™๋‹ค. ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฌธ์ œ๋Š” ๊ทธ๋ƒฅ ํ‹€๋ ค๋ฒ„๋ฆฐ๋‹ค.

๋˜ํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์กฐํ•ฉ ํ•จ์ˆ˜์ธ ์•„๋ž˜ ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋กœ ๋ฌธ์ œ๊ฐ€ ํ’€๋ฆฌ์ง€ ์•Š๋Š”๋‹ค.

const getCombinations = function (arr, selectNum) {
  const results = [];
  if (selectNum === 1) return arr.map((value) => [value]);

  arr.forEach((fixed, index) => {
    const rest = arr.slice(index + 1);
    const combinations = getCombinations(rest, selectNum - 1);
    const attached = combinations.map((combination) => [fixed, ...combination]);
    results.push(...attached);
  });

  return results;
};

 

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