๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ์ธ๊ณต๋ถ€/Algorithm

[๋ฐฑ์ค€][Node.js] 1620๋ฒˆ : ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ ์ด๋‹ค์†œ

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2021. 7. 10.

 

 

Algorithm

-  ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ ์ด๋‹ค์†œ -

 


 

๋ฌธ์ œ

์ฒซ์งธ ์ค„์—๋Š” ๋„๊ฐ์— ์ˆ˜๋ก๋˜์–ด ์žˆ๋Š” ํฌ์ผ“๋ชฌ์˜ ๊ฐœ์ˆ˜ N์ด๋ž‘ ๋‚ด๊ฐ€ ๋งž์ถฐ์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ ธ. N๊ณผ M์€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ธ๋ฐ, ์ž์—ฐ์ˆ˜๊ฐ€ ๋ญ”์ง€๋Š” ์•Œ์ง€? ๋ชจ๋ฅด๋ฉด ๋ฌผ์–ด๋ด๋„ ๊ดœ์ฐฎ์•„. ๋‚˜๋Š” ์–ธ์ œ๋“ ์ง€ ์งˆ๋ฌธ์— ๋‹ตํ•ด์ค„ ์ค€๋น„๊ฐ€ ๋˜์–ด์žˆ์–ด.

 

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ํฌ์ผ“๋ชฌ์˜ ๋ฒˆํ˜ธ๊ฐ€ 1๋ฒˆ์ธ ํฌ์ผ“๋ชฌ๋ถ€ํ„ฐ N๋ฒˆ์— ํ•ด๋‹นํ•˜๋Š” ํฌ์ผ“๋ชฌ๊นŒ์ง€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์™€. ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์€ ๋ชจ๋‘ ์˜์–ด๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ์žˆ๊ณ , ์ฒซ ๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž์ด๊ณ , ๋‚˜๋จธ์ง€ ๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์–ด. ํฌ์ผ“๋ชฌ ์ด๋Ÿผ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 20์ด์•ผ. ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ์ด M๊ฐœ์˜ ์ค„์— ๋‚ด๊ฐ€ ๋งž์ถฐ์•ผํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์™€. ๋ฌธ์ œ๊ฐ€ ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ๋“ค์–ด์˜ค๋ฉด ํฌ์ผ“๋ชฌ ๋ฒˆํ˜ธ๋ฅผ ๋งํ•ด์•ผ ํ•˜๊ณ , ์ˆซ์ž๋กœ๋งŒ ๋“ค์–ด์˜ค๋ฉด, ํฌ์ผ“๋ชฌ ๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•ด์•ผํ•ด. ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ์ˆซ์ž๋Š” ๋ฐ˜๋“œ์‹œ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , N๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๊ณ , ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๋ฌธ์ž๋Š” ๋ฐ˜๋“œ์‹œ ๋„๊ฐ์— ์žˆ๋Š” ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„๋งŒ ์ฃผ์–ด์ ธ.

 

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ M๊ฐœ์˜ ์ค„์— ๊ฐ๊ฐ์˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ๋‹ต์„ ๋งํ•ด์คฌ์œผ๋ฉด ์ข‹๊ฒ ์–ด! ์ž…๋ ฅ์œผ๋กœ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์™”๋‹ค๋ฉด ๊ทธ ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์„, ๋ฌธ์ž๊ฐ€ ๋“ค์–ด์™”์œผ๋ฉด ๊ทธ ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋ผ.

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

 

๋ฌธ์ œ ํ’€๊ธฐ

const readline = require("readline");
const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
	input.push(line);
}).on("close", function () {
	input[0] = input[0].split(' ');
	let n = +input[0][0];
    let m = +input[0][1];
    let animalArr = Array(n + 1).fill(undefined);
    let animalObj = {};
    let result = [];

    for (let i = 1; i <= n; i++) {
	    let animal = input[i];
	    animalArr[i] = animal;
	    animalObj[animal] = i;
    }

    for (let i = n + 1; i <= n + m; i++) {
	    let quiz = input[i];
    	if (isNaN(+quiz)) console.log(animalObj[quiz]);
	    else console.log(animalArr[+quiz]);
    }
	    process.exit();
});

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€