[BOJ 2504] ๊ดํธ์ ๊ฐ
1. ๋ฌธ์ : Link
(์) X2
[์] X3
๊ฐ์ ๊ดํธ ์์ +
ex) ( ( ) [ [ ] ] )
[[]] = [] ์์ [] = 3 X 3
() + [[]] = 2 + 9 = 11
(()+[[]]) = 11 X 2 = 22
2. ํ์ด
์คํํ๋ ์์ผ๋ก case๋ก ๊น๋ํ๊ฒ ์ฒ๋ฆฌํ๊ธฐ
ํด๋น ๊ดํธ ์์์ ๊ณฑ์ ์ด ์ ์ง๋๋ ๊ฐ๋ ์ ์ด๋ป๊ฒ ๊ตฌํํ๋์ง๊ฐ ์ค์
์ฌ๊ธฐ์๋ (,[๊ฐ ์ถ๊ฐ๋ ๋๋ง๋ค ๊ฐ์ ๊ณฑํด์ฃผ๊ณ -> ์ ์ง
๊ทธ๋ฌ๋ค๊ฐ ),]๊ฐ ๋์์๋ ๋๋ ์ฃผ๊ธฐ
()๊ฐ์ ์์ด ๋์ฌ๋ ๊ฒฐ๊ตญ ์ต์ข ๊ฐ์ด ๊ฒฐ๊ณผ์ ๋ํด์ง๋ ๊ฐ๋
3. ์ฝ๋
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String in = scanner.next();
int mul = 1;
int result = 0;
Stack<Character> stack = new Stack<>();
for (int i = 0; i < in.length(); i++) {
switch (in.charAt(i)) {
case '(':
stack.push('(');
mul *= 2;
break;
case '[':
stack.push('[');
mul *= 3;
break;
case ')':
if (stack.isEmpty() || stack.peek() != '(') {
result = 0;
break;
}
if (in.charAt(i - 1) == '(') result += mul;
stack.pop();
mul /= 2;
break;
case ']':
if (stack.isEmpty() || stack.peek() != '[') {
result = 0;
break;
}
if (in.charAt(i - 1) == '[') result += mul;
stack.pop();
mul /= 3;
break;
}
}
System.out.println(!stack.isEmpty() ? 0 : result);
}
}
'๐ป Coding Problems Solving > Stack | Queue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ํ๋ฆฐํฐ (์๋ฐ java) (0) | 2023.02.22 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ฌ๋ฐ๋ฅธ ๊ดํธ (์๋ฐ java) (0) | 2022.11.25 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ (0) | 2022.04.15 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ง์ง์ด ์ ๊ฑฐํ๊ธฐ (0) | 2022.04.09 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ ๋งต๊ฒ (0) | 2022.04.08 |
์ต๊ทผ๋๊ธ