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

[๋ฐฑ์ค€ 1759๋ฒˆ] ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Baekjoon

[๋ฐฑ์ค€ 1759๋ฒˆ] ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs)

Lennon 2022. 5. 2. 12:59
728x90
๋ฐ˜์‘ํ˜•

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

 

1759๋ฒˆ: ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ

์ฒซ์งธ ์ค„์— ๋‘ ์ •์ˆ˜ L, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (3 ≤ L ≤ C ≤ 15) ๋‹ค์Œ ์ค„์—๋Š” C๊ฐœ์˜ ๋ฌธ์ž๋“ค์ด ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์ด๋ฉฐ, ์ค‘๋ณต๋˜๋Š” ๊ฒƒ์€ ์—†๋‹ค.

www.acmicpc.net

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 === 2) {
    rl.close();
  }
}).on('close', function () {
  const collection = ['a', 'e', 'i', 'o', 'u'];
  const [L, C] = input[0].split(' ').map(Number);
  const solution = input[1].split(' ').sort();

  const combinationResult = getCombinations(solution, L);

  const answer = combinationResult
    .filter((v) => {
      const collectionLength = v.filter((ele) => collection.includes(ele)).length;
      const consonantLength = L - collectionLength;

      if (collectionLength >= 1 && consonantLength >= 2) {
        return v;
      }
    })
    .map((v) => v.join(''));

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

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