치춘짱베리굿나이스
[백준] 9523 본문
Arithmetic with Morse
문제
Morse code is a method used to transmit text messages as a series of dots “.” and dashes “-”. For example, the letter “A” is represented with “.-” and the letter “B” with “-...”. This code has been used for several years in the army and civil applications, but you are going to use it to do math.
With this in mind, we assign values to dots and dashes, and to save space we use two additional characters. The following table shows the four allowed characters and their values.
A Morse number is a string that only contains the above four characters; its value is the sum of the values assigned to each individual character. As an example, the value of “=.-..” is 10+1+5+1+1 = 18. Notice that each Morse number represents a unique value, but there are values that can be represented with several Morse numbers. For instance, there are three Morse numbers with value 3: “...”, “.:” and “:.”.
Well, numbers are ready. To form expressions we also need operators. We consider two arithmetic operators: “+” represents addition, while “*” represents multiplication. A Morse expression is a sequence of strings that interleaves Morse numbers and operators, that starts and ends with a Morse number, and contains at least one operator. Morse expressions can be evaluated by replacing each Morse number by its value, and then evaluating the obtained mathematical expression using the common operators precedence and associativity. For example, the value of the Morse expression “=.-.. + ... * :.” is 18 + 3 × 3 = 18 + (3 × 3) = 27. Given a Morse expression, print its value.
입력
The first line contains an integer N (1 ≤ N ≤ 4) representing the number of operators in the Morse expression. The second line contains 2N + 1 non-empty strings representing the Morse expression. The line interleaves Morse numbers and operators, being the first and last strings Morse numbers. Each Morse number is at most 7 characters long, where each character is either “.”, “-”, “:” or “=”. Each operator is either “+” or “*”.
출력
Output a line with an integer representing the value of the Morse expression.
풀이
const morse = () => {
let input = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\\n")[1];
let stack = [];
let num = 0;
for (let i = 0; i < input.length; i++) {
if (input[i] === "=") num += 10;
else if (input[i] === "-") num += 5;
else if (input[i] === ":") num += 2;
else if (input[i] === ".") num += 1;
else if (input[i] === "*") {
let tmp = 0;
i += 2;
if (num) stack.push(num);
while (input[i] != " " && i < input.length) {
if (input[i] === "=") tmp += 10;
else if (input[i] === "-") tmp += 5;
else if (input[i] === ":") tmp += 2;
else if (input[i] === ".") tmp += 1;
i++;
}
stack.push(tmp * stack.pop());
num = 0;
} else if (input[i] === "+") {
stack.push(num);
num = 0;
}
if (i === input.length - 1) stack.push(num);
}
num = 0;
while (stack.length > 0) num += stack.pop();
console.log(num);
};
morse();
반성회
곱연산의 연산 순서를 어떻게 맞춰야할지 감이 안와서 * 기호를 만나면 그냥 그자리에서 while문을 돌아서 뒤의 숫자를 바로 만들어버렸다
시간제한도 비교적 넉넉하고 테스트케이스도 엄청 짧아서 다행히 통과되긴 했는데 뭔가 정석적인 풀이방법은 아닌듯 싶다
'Javascript + Typescript > 자바스크립트로 알고리즘풀기' 카테고리의 다른 글
[백준] 11022 (0) | 2022.02.09 |
---|---|
[백준] 2164 (0) | 2022.02.09 |
[백준] 10866 (0) | 2022.02.09 |
[백준] 10845 (0) | 2022.02.09 |
[백준] 4992 (0) | 2022.02.09 |