풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
static int count = 0; // 정답
public static void main(String[] args) throws NumberFormatException, IOException {
int a = 0; // () 열고 닫기 여부
int b = 0; // [] 열고 닫기 여부
int state = 1;
String c = ""; // 첫 시작 괄호 체크
boolean check = true; // 올바른 문자열 체크 변수
int change = 1;
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
BufferedReader br = new BufferedReader ( new InputStreamReader (System.in));
String str = br.readLine();
String[] arr = str.split(""); // 입력받은 string 배열에 담기
// 올바른 문자열 확인
for(int i=0; i<arr.length; i++) {
list.add(arr[i]);
list2.add(arr[i]);
}
String answer = Del(list);
if("YES".equals(answer)) {
String strOne = "";
String strTwo = "";
for(int i=0; i<list2.size()-1; i++) {
strOne = list2.get(i).toString();
strTwo = list2.get(i+1).toString();
if("(".equals(strOne)) {
if(")".equals(strTwo)) {
list2.set(i, Integer.toString(2));
list2.remove(i+1);
}
}else if("[".equals(strOne)) {
if("]".equals(strTwo)) {
list2.set(i, Integer.toString(3));
list2.remove(i+1);
}
}
}
Count(list2, state);
}else {
System.out.println(count);
}
}
// 올바른 문자열 찾기
private static String Del(ArrayList<String> list) {
String strOne = "";
String strTwo = "";
boolean check = true;
for(int i=0; i<list.size()-1; i++) {
strOne = list.get(i).toString();
strTwo = list.get(i+1).toString();
if("(".equals(strOne)) {
if(")".equals(strTwo)) {
list.remove(i);
list.remove(i);
check = false;
}
}else if("[".equals(strOne)) {
if("]".equals(strTwo)) {
list.remove(i);
list.remove(i);
check = false;
}
}
}
if(check == false) {
return Del(list);
}else {
if(list.size() == 0) {
return "YES";
}else {
return "NO";
}
}
}
private static void Count(ArrayList<String> list, int state) {
String strOne = "";
String strTwo = "";
String strThree = "";
int num = 0;
if(state == 1) {
for(int i=0; i<list.size()-2; i++) {
strOne = list.get(i).toString();
strTwo = list.get(i+1).toString();
strThree = list.get(i+2).toString();
if("(".equals(strOne)) {
if(")".equals(strThree)) {
num = 2 * Integer.parseInt(strTwo);
list.remove(i);
list.remove(i);
list.set(i, Integer.toString(num));
i--;
}
}else if("[".equals(strOne)) {
if("]".equals(strThree)) {
num = 3 * Integer.parseInt(strTwo);
list.remove(i);
list.remove(i);
list.set(i, Integer.toString(num));
i--;
}
}
}
}else {
for(int i=0; i<list.size()-1; i++) {
strOne = list.get(i).toString();
strTwo = list.get(i+1).toString();
if(!(("(".equals(strOne)) || (")".equals(strOne)) || ("[".equals(strOne)) || ("]".equals(strOne)))) {
if(!(("(".equals(strTwo)) || (")".equals(strTwo)) || ("[".equals(strTwo)) || ("]".equals(strTwo)))) {
num = Integer.parseInt(strOne) + Integer.parseInt(strTwo);
list.remove(i);
list.set(i, Integer.toString(num));
i--;
}
}
}
}
if(list.size() > 1) {
Count(list, state * -1);
}else {
count = Integer.parseInt(list.get(0));
System.out.println(count);
}
}
}
'코딩 문제 > 백준' 카테고리의 다른 글
백준 - 2891번 (0) | 2021.03.15 |
---|---|
백준 - 2890번 (0) | 2021.03.15 |
백준 - 1780번 (0) | 2021.03.15 |
백준 - 1769번 (0) | 2021.03.15 |
백준 - 1074번 (0) | 2021.03.15 |