Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60318ac

Browse files
committed
배수빈: [BOJ] 2504 괄호의 값_250207
1 parent b7f049e commit 60318ac

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

‎BOJ/1000-5000번/SB_2504.java‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayDeque;
5+
import java.util.Deque;
6+
7+
public class SB_2504 {
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
String line = br.readLine();
11+
12+
Deque<Character> stack = new ArrayDeque<>();
13+
int ans = 0;
14+
int tmp = 1;
15+
16+
for (int i = 0; i < line.length(); i++) {
17+
char cur = line.charAt(i);
18+
switch (cur) {
19+
case '(':
20+
stack.addFirst(cur);
21+
tmp*=2;
22+
break;
23+
case '[':
24+
stack.addFirst(cur);
25+
tmp*=3;
26+
break;
27+
case ')':
28+
if (stack.isEmpty() || stack.peekFirst() != '('){
29+
System.out.println(0);
30+
return;
31+
}
32+
if (line.charAt(i - 1) == '(') ans+=tmp;
33+
tmp/=2;
34+
stack.pollFirst();
35+
break;
36+
case ']':
37+
if (stack.isEmpty() || stack.peekFirst() != '['){
38+
System.out.println(0);
39+
return;
40+
}
41+
if (line.charAt(i-1)=='[') ans+=tmp;
42+
tmp/=3;
43+
stack.pollFirst();
44+
break;
45+
}
46+
}
47+
48+
// 남아있는 괄호 여부 체크
49+
if (stack.isEmpty()) System.out.println(ans);
50+
else System.out.println(0);
51+
}
52+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /