๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/5430
๋ฌธ์ ํ์ด
R ์ฐ์ฐ์ ํด์ผํ ์ ์ค์ ๋ฐฐ์ด์ ์๋ค๋ฅผ ๋ณ๊ฒฝํ๊ฒ ๊ณ์ฐ์๋ ์ฐจ์์์ ํจ์จ์ ์ด์ง ๋ชปํ๋ค. ๋ฐ๋ผ์ ์๋ณธ ๋ฐฐ์ด์ ์์์ ์์๋ฅผ ๋ค์ง๊ธฐ ๋ณด๋ค๋ ์์์ ์์๋ฅผ ์ ๊ฑฐ or ๋ค์์ ์์๋ฅผ ์ ๊ฑฐ ํ๋ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ๋ฅผ ํ์ด์ผ ์๊ฐ์ด๊ณผ ์์ด ๋ฌธ์ ๋ฅผ ํ ์ ์๋ค. ๋๋ direction ์ด๋ผ๋ ๋ณ์์ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ๋ฐฐ์ด์ ์์๋ฅผ ์ ๊ฑฐํ๋ ๋ฐฉํฅ์ ํ์ ํ์ฌ ์ด๋ฅผ ์งํํ์๋ค. ๋ฌธ์ ๋ฅผ ๋ค ํ๊ณ ๋์ ๋ณด๋ ํ์ด๊ฐ ๊ฝค๋ ๊ธด๊ฑฐ ๊ฐ์๋ฐ ์ถํ ์๊ฐ๋๋ฉด ์ค์ฌ๋ด์ผ๊ฒ ๋ค. ( ๋ค๋ฅธ์ฌ๋ํ์ด : https://tesseractjh.tistory.com/250 )
let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
let testCaseCnt = Number(input.shift());
let answer = "";
for (let i = 0; i < input.length; i += 3) {
let isError = false;
let direction = "left";
let commendArr = input[i].split("");
let numCnt = Number(input[i + 1]);
let numArr = input[i + 2];
// ๋ฐฐ์ด์ด ์ ์ด์ ๋น์ด์๋ ๊ฒฝ์ฐ
if (numCnt === 0) {
if (commendArr.includes("D")) {
answer += "error\n";
} else {
answer += "[]\n";
}
continue;
}
// ์์ฌ์ด๋ [] ์ญ์ ํ ๋ฐฐ์ด๋ก ๋ง๋ฌ
numArr = numArr.slice(1, numArr.length - 1);
numArr = numArr.split(",").map((n) => Number(n));
for (let j = 0; j <= commendArr.length - 1; ++j) {
let commend = commendArr[j];
if (commend === "R") {
// ๋ค์ง๊ธฐ
direction = direction === "left" ? "right" : "left";
} else if (commend === "D") {
if (numArr.length === 0) {
isError = true;
break;
}
if (direction === "left") {
numArr.shift();
} else if (direction === "right") {
numArr.pop();
}
}
}
if (isError) {
answer += "error\n";
} else {
if (direction === "left") {
answer += `[${numArr.join(",")}]\n`;
} else if (direction === "right") {
answer += `[${numArr.reverse().join(",")}]\n`;
}
}
}
console.log(answer);
'๐Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค[JS] > 7762๋ฒ ์ด์ค ์ฐ์ ์์ ํ (0) | 2025.01.12 |
---|---|
๋ฐฑ์ค[node.js] > 1931๋ฒ (0) | 2025.01.09 |
๋ฐฑ์ค[node.js] > 9461๋ฒ ํ๋๋ฐ ์์ด (0) | 2025.01.05 |
๋ฐฑ์ค[node.js] > 11399๋ฒ ATM (0) | 2025.01.02 |
๋ฐฑ์ค[node.js] > 11279๋ฒ ์ต๋ ํ (0) | 2024.12.31 |