From e7adf67995742f225c224c0f446dd38b5cff6995 Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Mon, 1 Sep 2025 08:12:55 +0900 Subject: [PATCH] =?UTF-8?q?[20250901]=20BOJ=20/=20G5=20/=20=EA=B4=84?= =?UTF-8?q?=ED=98=B8=EC=9D=98=20=EA=B0=92=20/=20=EC=9D=B4=EC=A4=80?= =?UTF-8?q?=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\355\230\270\354\235\230 \352\260\222.md" | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 "JHLEE325/202509/01 BOJ G5 \352\264\204\355\230\270\354\235\230 \352\260\222.md" diff --git "a/JHLEE325/202509/01 BOJ G5 \352\264\204\355\230\270\354\235\230 \352\260\222.md" "b/JHLEE325/202509/01 BOJ G5 \352\264\204\355\230\270\354\235\230 \352\260\222.md" new file mode 100644 index 00000000..e09ce74e --- /dev/null +++ "b/JHLEE325/202509/01 BOJ G5 \352\264\204\355\230\270\354\235\230 \352\260\222.md" @@ -0,0 +1,81 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String str = br.readLine(); + + Stack stack = new Stack<>(); + Stack values = new Stack<>(); + + for (int i = 0; i < str.length(); i++) { + char c = str.charAt(i); + + if (c == '(' || c == '[') { + stack.push(c); + values.push(-1); + } else { + if (stack.isEmpty()) { + System.out.println(0); + return; + } + + if (c == ')') { + if (stack.peek() != '(') { + System.out.println(0); + return; + } + stack.pop(); + + int sum = 0; + while (!values.isEmpty() && values.peek() != -1) { + sum += values.pop(); + } + if (values.isEmpty() || values.peek() != -1) { + System.out.println(0); + return; + } + values.pop(); + values.push(sum == 0 ? 2 : sum * 2); + } else if (c == ']') { + if (stack.peek() != '[') { + System.out.println(0); + return; + } + stack.pop(); + + int sum = 0; + while (!values.isEmpty() && values.peek() != -1) { + sum += values.pop(); + } + if (values.isEmpty() || values.peek() != -1) { + System.out.println(0); + return; + } + values.pop(); + values.push(sum == 0 ? 3 : sum * 3); + } + } + } + + if (!stack.isEmpty()) { + System.out.println(0); + return; + } + + int result = 0; + while (!values.isEmpty()) { + if (values.peek() == -1) { + System.out.println(0); + return; + } + result += values.pop(); + } + + System.out.println(result); + } +} + +```