undefined
[백준 - Node.js] 암호 만들기 1759번 본문
반응형
문제 설명
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 = [];
let count = 0;
let n = 0;
rl.on('line', function (line) {
if (!count) {
let arr = line.split(' ');
n = +arr[0];
} else {
// 미리 정렬
input = line.split(' ').sort();
}
count++;
}).on('close', function () {
solution(input);
process.exit();
});
// 필터함수 => 모음 기준으로 최소 1개 그래고 자음의 자리 최소 2개 남겨줌
const filter = arr => {
const vowel = ['a', 'e', 'i', 'o', 'u'];
let ctn = 0;
for (let alpha of arr) {
if (vowel.includes(alpha)) ctn++;
}
if (ctn >= 1 && ctn <= n - 2) return true;
return false;
};
const solution = input => {
// 초기세팅
const visited = new Array(input.length - 1).fill(false);
const arr = [];
let answer = [];
// dfs
const dfs = (count, idx) => {
// base case => 자릿수와 필터함수 기준
if (count === n && filter(arr)) {
answer.push(arr.join(''));
return;
}
// input순회
for (let i = idx; i < input.length; i++) {
// 방문X 백트래킹
if (!visited[i]) {
arr.push(input[i]);
visited[i] = true;
dfs(count + 1, i);
arr.pop();
visited[i] = false;
}
}
};
dfs(0, 0);
// 다시정렬
console.log(answer.sort().join('\n'));
};
반응형
'Coding Test' 카테고리의 다른 글
[백준 - Node.js] 토마토 7576번 (0) | 2022.10.02 |
---|---|
[백준 - node.js] 스타트와 링크 14889번 (1) | 2022.09.23 |
[백준 - node.js] 1, 2, 3 더하기 9095번 (0) | 2022.09.22 |
[백준 - node.js] 외판원 순회2 10971번 (1) | 2022.09.21 |
[백준] 다음 순열 10972번 - Javascript / Node.js (0) | 2022.09.11 |
Comments