본문 바로가기

JS 코딩테스트/문제풀이

[자바스크립트] 18679 Banana

반응형

18679번: Banana

18679번: Banana

문제

After NASA discovered water on Mars, they decided to expand their exploration hoping to find some alien intelligence on the planet. After months of exploration, they were actually surprised to find out that the planet has been inhabited by minions.

NASA started communications with the minions and the first message they received was Mo amo Banana. At first, it was really hard to decipher the message but after sometime they managed to workout a dictionary that maps English words to Minionese words. You are going to help NASA build the translator to ease their communication with Minions for the good and prosperity of mankind and minionkind.

 

 

입력

The first line of input will contain a single integer N, the number of words in the dictionary (1 ≤ N ≤ 100). The following N lines will each contain a sentence of the format x = y where x is an English word and y is a Minionese word. The next line will contain an integer T, the number of test cases (1 ≤ T ≤ 100). Each test case will start with a line containing an integer K, the number of words in the sentence (1 ≤ K ≤ 100) and the next line will contain K space separated English words. All the English words in the test cases exist in the defined dictionary. Also, all the words consist only of English alphabet, and will be at most 20 characters long.

 

 

출력

For each test case, print a single line containing the space separated Minionese words after translation.

 

 

예제 입력 1

4
I = mo
love = amo
icecream = gelatooo
banana = banana
2
3
I love banana
3
I love icecream

 

 

예제 출력 1

mo amo banana
mo amo gelatooo

 

 

자료구조

정수: n, t

② 문자열 배열: input
③ Map 객체: map

 

 

알고리즘

Map 생성: input 배열에서 원소를 꺼내와서 등호(=)를 기준으로 나누어 맵에 키-값 쌍으로 저장한다.

단어 변환:

  • 현재 쿼리에 해당하는 단어들을 공백을 기준으로 나누어 배열로 만든다.
  • 각 단어를 Map에서 찾아서 대응하는 값으로 변환한다.
  • 변환된 값들을 공백으로 연결하여 출력

 

 

소스 코드 1

const fs = require("fs");

let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");

let n = +input.shift();

const map = new Map();
for (let i = 0; i < n; i++) {
  const [x, y] = input.shift().split(" = ");
  map.set(x, y);
}

const t = +input.shift();

for (let i = 0; i < t; i++) {
  input.shift();
  console.log(
    input
      .shift()
      .split(" ")
      .map((word) => map.get(word))
      .join(" "),
  );
}

132ms

 

 

 

✔ 출처

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

반응형