본문 바로가기
알고리즘 (with. 백준 문제풀이)/Hash Map & Set

백준 17219번 [비밀번호 찾기]

by 천릉객 2023. 9. 19.
const input = require('fs')
    .readFileSync('/dev/stdin')
    .toString()
    .trim()
    .split('\n')

const [n, m] = input.shift().split(' ')
const map = new Map()

for(let i=0; i<n; i++){
    map.set(input[i].split(' ')[0], input[i].split(' ')[1])
}

let answer = []

for(let i=n; i<input.length; i++){
    answer.push(map.get(input[i]))
}

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

자바스크립트에도 Map()이 있는 줄 몰랐었다. 사용하니까 어렵진 않았음

input[i].split(' ')[0], input[i].split(' ')[1] : input[i]를 나누고 나면 두개가 나오는데 그 두개를 각각 set에 키-밸류 인자로 전달.

input에서 map으로 넘어갈 때, n줄까지는 map에 입력하고 n 다음 줄 부터는 찾는 대상으로 처리까지 하면 쉽게 풀리는 문제