Skip to content

Commit

Permalink
CU_4624
Browse files Browse the repository at this point in the history
  • Loading branch information
likppi10 committed Nov 7, 2021
1 parent 99b2852 commit 7ef0ba6
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Algorithm_CU/CU_4624_괄호의값.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package Stack;

import java.util.Scanner;
import java.util.Stack;

public class CU_4624_괄호의값 {

static Stack<Character> stack = new Stack<Character>();
public static void main(String args[]) {

Scanner sc = new Scanner(System.in);
String parenthesis = sc.nextLine();
char[] array = parenthesis.toCharArray();

calculate(array);
}
static void calculate(char[] array) {
int finalresult = 0;
int result = 0;
int circleopen = 0;
int squareopen = 0;

for(int i=0; i<array.length; i++) {
stack.push(array[i]);
}
char prev = stack.pop();
if(prev == ')') {
result = 2;
}else if(prev == ']') {
result = 3;
}
char now = '0';
for(int j=1; j<stack.size(); j++) {
now = stack.pop();
if(now == ')') {
if(prev == '(' && prev == '[') {
result = result+2;
}else {
result = result*2;
}
circleopen++;
prev = now;
}else if(now == '(') {
result = result*1;
prev = now;
circleopen--;
}else if(now == ']') {
if(prev == '(' && prev == '[') {
result = result+3;
}else {
result = result*3;
}
squareopen++;
prev = now;
}else if(now == '[') {
result = result*1;
prev = now;
squareopen--;
}
}
System.out.print(result);

}
}

0 comments on commit 7ef0ba6

Please sign in to comment.