문제
입력 및 출력
풀이
import java.io.*;
import java.util.*;
class Main {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] input = br.readLine().toCharArray();
Stack<Character> stack = new Stack<>();
int result = 0;
int value = 1;
for(int i = 0; i < input.length; i++) {
if(input[i] == '(') {
stack.push(input[i]);
value *= 2;
}
if(input[i] == '[') {
stack.push(input[i]);
value *= 3;
}
if(input[i] == ')') {
if(stack.isEmpty() || stack.peek() != '(') {
break;
}else if(input[i-1] == '(') {
result += value;
}
stack.pop();
value /= 2;
}else if(input[i] == ']') {
if(stack.isEmpty() || stack.peek() != '[') {
break;
}else if(input[i-1] == '[') {
result += value;
}
stack.pop();
value /= 3;
}
}
if(stack.isEmpty() == false) {
System.out.println(0);
}else {
System.out.println(result);
}
}
}
결과 및 해결방법
[결과]