๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 9523_Arithmetic with Morse

๋ฟŒ์•ผ._. 2025. 7. 15. 12:08
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/9523)

< Arithmetic with Morse >

 

๋ฌธ์ œ ํ’€์ด 

 

1) ๋ชจ์Šค ๋ถ€ํ˜ธ๋ฅผ ์ˆซ์ž๋กœ ๋ฐ”๊พธ๊ธฐ

2) ๊ณฑ์…ˆ

3) ๋”ํ•˜๊ธฐ

์ˆœ์œผ๋กœ ๋ชจ์Šค ํ‘œํ˜„์‹์„ ์ˆซ์ž๋กœ ๊ตฌํ•œ๋‹ค.

 

my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class _9523_ { // Arithmetic with Morse

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		int N = Integer.parseInt(bf.readLine());
		String str = bf.readLine();

		str = str.replace(" ", "");
		Stack<String> stack = new Stack<>();
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == '.') {
				int num = 0;
				if (stack.size() > 0 && !stack.peek().equals("+") && !stack.peek().equals("*")) {
					num = Integer.parseInt(stack.pop());
				}
				stack.add(Integer.toString(num + 1));
			} else if (str.charAt(i) == '-') {
				int num = 0;
				if (stack.size() > 0 && !stack.peek().equals("+") && !stack.peek().equals("*")) {
					num = Integer.parseInt(stack.pop());
				}
				stack.add(Integer.toString(num + 5));
			} else if (str.charAt(i) == ':') {
				int num = 0;
				if (stack.size() > 0 && !stack.peek().equals("+") && !stack.peek().equals("*")) {
					num = Integer.parseInt(stack.pop());
				}
				stack.add(Integer.toString(num + 2));
			} else if (str.charAt(i) == '=') {
				int num = 0;
				if (stack.size() > 0 && !stack.peek().equals("+") && !stack.peek().equals("*")) {
					num = Integer.parseInt(stack.pop());
				}
				stack.add(Integer.toString(num + 10));
			} else {
				stack.add(Character.toString(str.charAt(i)));
			}
		}

		Stack<Integer> result = new Stack<>();

		int a = Integer.parseInt(stack.pop());
		while (!stack.isEmpty()) {
			String temp = stack.pop();
			int b = Integer.parseInt(stack.pop());

			if (temp.equals("+")) {
				result.add(a);
				result.add(b);
			} else if (temp.equals("*")) {
				result.add(a * b);
			}
			a = result.pop();
		}

		int answer = a;
		while (!result.isEmpty()) {
			answer += result.pop();
		}
		System.out.println(answer);
	}
}
๋ณ€์ˆ˜)
N : ์—ฐ์‚ฐ์ž ์ˆ˜
str : ๋ชจ์Šค ํ‘œํ˜„์‹
stack : ๋ชจ์Šค ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•œ ๊ฐ’์„ ์ €์žฅ
result : ๊ณฑํ•˜๊ธฐ ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ์ €์žฅ
answer : ์ตœ์ข… ๋ชจ์Šค ํ‘œํ˜„์‹ ๊ฐ’

 

์—ฐ์‚ฐ์ž ์ˆ˜์™€ ๋ชจ์Šค ํ‘œํ˜„์‹์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋ชจ์Šค ํ‘œํ˜„์‹์—์„œ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๊ณ  ์ˆœ์ฐจ ํƒ์ƒ‰ํ•˜๋ฉฐ ๋ชจ์Šค ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ stack์— ์ €์žฅํ•œ๋‹ค. ์ด๋•Œ, ๋”ํ•˜๊ธฐ์™€ ๊ณฑ์…ˆ ํ‘œ์‹œ๋Š” ๋ณ€ํ™˜ ์—†์ด ๊ทธ๋Œ€๋กœ stack์— ์ €์žฅํ•œ๋‹ค.

 

stack์— ์žˆ๋Š” ๊ฐ’์„ pop ํ•˜๋ฉด์„œ ๊ณฑ์…ˆ ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ์„ ๋•Œ ๊ณฑ์…ˆ ๊ณ„์‚ฐ ํ›„ result stack์— ์ €์žฅํ•œ๋‹ค. ์ตœ์ข… result stack์— ์žˆ๋Š” ๊ฐ’๋“ค์„ ๋‹ค ๋”ํ•œ ํ›„ ์ถœ๋ ฅํ•œ๋‹ค.



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 17585_Circuit Math  (2) 2025.07.17
[Baekjoon] 8594_Program  (0) 2025.07.16
[Baekjoon] 14210_Kartomat  (0) 2025.07.14
[Baekjoon] 27060_Vertical Histogram  (2) 2025.07.11
[Baekjoon] 32076_Easy as ABC  (2) 2025.07.10