Algorithm
- ์๋ฆฌ์ผ ๋ฒํธ -
๋ฌธ์
๋ค์์ด๋ ๊ธฐํ๋ฅผ ๋ง์ด ๊ฐ์ง๊ณ ์๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ๊ฐ์ ๊ธฐํ๋ ๋ชจ๋ ๋ค๋ฅธ ์๋ฆฌ์ผ ๋ฒํธ๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ๋ค์์ด๋ ๊ธฐํ๋ฅผ ๋นจ๋ฆฌ ์ฐพ์์ ๋นจ๋ฆฌ ์ฌ๋๋ค์๊ฒ ์ฐ์ฃผํด์ฃผ๊ธฐ ์ํด์ ๊ธฐํ๋ฅผ ์๋ฆฌ์ผ ๋ฒํธ ์์๋๋ก ์ ๋ ฌํ๊ณ ์ ํ๋ค.
๋ชจ๋ ์๋ฆฌ์ผ ๋ฒํธ๋ ์ํ๋ฒณ ๋๋ฌธ์ (A-Z)์ ์ซ์ (0-9)๋ก ์ด๋ฃจ์ด์ ธ ์๋ค.
์๋ฆฌ์ผ๋ฒํธ A๊ฐ ์๋ฆฌ์ผ๋ฒํธ B์ ์์ ์ค๋ ๊ฒฝ์ฐ๋ ๋ค์๊ณผ ๊ฐ๋ค.
1. A์ B์ ๊ธธ์ด๊ฐ ๋ค๋ฅด๋ฉด, ์งง์ ๊ฒ์ด ๋จผ์ ์จ๋ค.
2. ๋ง์ฝ ์๋ก ๊ธธ์ด๊ฐ ๊ฐ๋ค๋ฉด, A์ ๋ชจ๋ ์๋ฆฌ์์ ํฉ๊ณผ B์ ๋ชจ๋ ์๋ฆฌ์์ ํฉ์ ๋น๊ตํด์ ์์ ํฉ์ ๊ฐ์ง๋ ๊ฒ์ด ๋จผ์ ์จ๋ค. (์ซ์์ธ ๊ฒ๋ง ๋ํ๋ค)
3. ๋ง์ฝ 1, 2๋ฒ ๋ ์กฐ๊ฑด์ผ๋ก๋ ๋น๊ตํ ์ ์์ผ๋ฉด, ์ฌ์ ์์ผ๋ก ๋น๊ตํ๋ค. ์ซ์๊ฐ ์ํ๋ฒณ๋ณด๋ค ์ฌ์ ์์ผ๋ก ์๋ค.
์๋ฆฌ์ผ์ด ์ฃผ์ด์ก์ ๋, ์ ๋ ฌํด์ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ์ถ๋ ฅ ์์
๋ฌธ์ ํ๊ธฐ
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
input.push(line.toString());
}).on("close", function () {
input.shift();
input.sort((a, b) => a.length - b.length);
for (let i = 0; i < input.length; i++) {
let sum = 0;
for (let j = 0; j < input[i].length; j++) {
if (!isNaN(input[i][j])) sum += +input[i][j];
}
input[i] = [input[i], sum];
}
let result = [];
let i = 0;
while (true) {
// ๋จ์ด์ ๊ธธ์ด
let strLen = input.filter((el) => el[0].length === input[i][0].length);
if (strLen.length === 1) {
result = result.concat(strLen[0][0]);
} else {
// ์ซ์์ ๋ง์
let sumLen = strLen.sort((a, b) => a[1] - b[1]);
let j = 0;
while (true) {
let sameLen = sumLen.filter((el) => el[1] === sumLen[j][1]);
if (sameLen.length === 1) {
result = result.concat(sameLen[0][0]);
} else {
sameLen.sort();
result = result.concat(sameLen.map((el) => el[0]));
}
j += sameLen.length;
if (j >= sumLen.length) break;
}
}
i += strLen.length;
if (i >= input.length) break;
}
console.log(result.join('\n'));
process.exit();
});
'๊ฐ์ธ๊ณต๋ถ > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค][Node.js] 1475๋ฒ : ๋ฐฉ ๋ฒํธ (0) | 2021.07.03 |
---|---|
[๋ฐฑ์ค][Node.js] 1463๋ฒ : 1๋ก ๋ง๋ค๊ธฐ (0) | 2021.07.03 |
[๋ฐฑ์ค][Node.js] 1427๋ฒ : ์ํธ์ธ์ฌ์ด๋ (0) | 2021.06.30 |
[๋ฐฑ์ค][Node.js] 1085๋ฒ : ์ง์ฌ๊ฐํ์์ ํ์ถ (0) | 2021.06.29 |
[๋ฐฑ์ค][Node.js] 1057๋ฒ : ํ ๋๋จผํธ (0) | 2021.06.28 |
๋๊ธ