카테고리 없음

[알고리즘] 2504번

고규마 2017. 12. 20. 12:24

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Stack;


public class Main {


static boolean bool = true;

static Stack stack = new Stack();

static Stack<Integer> stackInteger = new Stack<Integer>();


public static void main(String[] args) throws IOException {


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String str = br.readLine();


int solve = 0;


    //1. 불가능한 경우가 아니면 push()진행

for (int i = 0; i < str.length(); i++) {

if (bool) {

String c = str.substring(i, i + 1);

push(c);

} else {

break;

}

}



   //2. 출력하기

if (stack.size() > stackInteger.size() || !bool) {

System.out.println(0);

} else {

for (int k = 0; k < stackInteger.size(); k++) {

solve += stackInteger.get(k);

}

System.out.println(solve);

}


}


private static void push(String c) {

if (c.equals("(") || c.equals("[")) {

stack.push(c);

} else {

pop(c);

}

}


private static void pop(String c) {

int temp = 0;

if (c.equals(")")) {

if (stack.isEmpty() || !stack.peek().equals("(")) {

while (stack.isEmpty() || !stack.peek().equals("(")) {

if (stack.isEmpty() || stack.peek().equals("[")) {

bool = false;

break;

} else {

stack.pop();

temp += stackInteger.pop();

}

}

temp = temp * 2;

} else {

temp = 2;

}

if (bool) {

stack.pop();

stack.push(temp);

stackInteger.push(temp);

}


} else {

if (!stack.peek().equals("[")) {

while (stack.isEmpty() || !stack.peek().equals("[")) {

if (stack.isEmpty() || stack.peek().equals("(")) {

bool = false;

break;

} else {

stack.pop();

temp += stackInteger.pop();

}

}

temp = temp * 3;

} else {

temp = 3;

}

if (bool) {

stack.pop();

stack.push(temp);

stackInteger.push(temp);

}


}


}


}