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

[๋ฐฑ์ค€][Node.js] 5218๋ฒˆ : ์•ŒํŒŒ๋ฒณ ๊ฑฐ๋ฆฌ

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2021. 11. 9.

 

 

Algorithm

-  ์•ŒํŒŒ๋ฒณ ๊ฑฐ๋ฆฌ -

 


 

๋ฌธ์ œ

๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋‘ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ ๋‹จ์–ด์— ํฌํ•จ๋œ ๋ชจ๋“  ๊ธ€์ž์˜ ์•ŒํŒŒ๋ฒณ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๋‘ ๊ธ€์ž x์™€ y ์‚ฌ์ด์˜ ์•ŒํŒŒ๋ฒณ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด, ๋จผ์ € ๊ฐ ์•ŒํŒŒ๋ฒณ์— ์ˆซ์ž๋ฅผ ํ• ๋‹นํ•ด์•ผ ํ•œ๋‹ค. 'A'=1, 'B' = 2, ..., 'Z' = 26. ๊ทธ ๋‹ค์Œ y ≥ x์ธ ๊ฒฝ์šฐ์—๋Š” y-x, y < x์ธ ๊ฒฝ์šฐ์—๋Š” (y+26) - x๊ฐ€ ์•ŒํŒŒ๋ฒณ ๊ฑฐ๋ฆฌ๊ฐ€ ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 'B'์™€ 'D' ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋Š” 4 - 2 = 2์ด๊ณ , 'D'์™€ 'B' ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋Š” (2+26) - 4 = 24์ด๋‹ค.

 

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

 

๋ฌธ์ œ ํ’€๊ธฐ

let fs = require("fs");
let input = fs.readFileSync("/dev/stdin")
	.toString()
	.split("\n");
let alpha = "abcdefghijklmnopqrstuvwxyz".toUpperCase().split("");
alpha = new Map(alpha.map((el, idx) => [el, idx + 1]));

for (let i = 1; i <= +input[0]; i++) {
	let test = input[i].split(" ");
	let result = "Distances: ";
	for (let j = 0; j < test[0].length; j++) {
		let str1 = alpha.get(test[0][j]);
		let str2 = alpha.get(test[1][j]);
		if (str1 <= str2) result += (str2 - str1) + ' ';
		else result += (str2 + 26 - str1) + ' ';
	}
	console.log(result.trim());
}

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€