- 풀이
(이면 2, [이면 3으로 temp에 곱해주면서 닫힌괄호가 나올 시 temp를 더 해주고 닫힌 괄호의 값을 temp에서 다시 나눠주는 방식을 사용했다.
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Stack;
public class Main {
static String s;
static int answer;
public static void main(String[] args) throws Exception {
SetData();
System.out.println(answer);
}
// 데이터
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
s = in.nextLine();
answer = 0;
SaveAnswer();
}
private static void SaveAnswer() {
Stack<Integer> stack=new Stack<>();
int temp=1;
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i)=='(') {
stack.add(2);
temp*=2;
}else if(s.charAt(i)=='[') {
stack.add(3);
temp*=3;
}else {
// 여는 괄호 X
if(stack.size()==0) {
answer=0;
break;
}
// 여는 괄호 X && 닫는 괄호 X
if(s.charAt(i)!=']' && s.charAt(i)!=')') {
answer=0;
break;
}
// 괄호 매칭 X
if((s.charAt(i)==']' && stack.peek()==2) || (s.charAt(i)==')' && stack.peek()==3)) {
answer=0;
break;
}
if((s.charAt(i-1)=='(' && s.charAt(i) == ')') || (s.charAt(i-1)=='[' && s.charAt(i) == ']')) {
answer+=temp;
}
temp/=stack.pop();
}
}
if(stack.size()!=0) {
answer=0;
}
}
}
class Node implements Comparable<Node> {
long age;
int index;
public Node(long age, int index) {
this.age= age;
this.index = index;
}
@Override
public int compareTo(Node o) {
// TODO Auto-generated method stub
return Long.compare(this.age, o.age);
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 14569번 : 시간표 짜기 (0) | 2021.08.02 |
---|---|
[BOJ/JAVA] 백준 1303번 : 전쟁 - 전투 (0) | 2021.08.02 |
[BOJ/JAVA] 백준 13305번 : 주유소 (0) | 2021.07.12 |
[Programmers/JAVA] 2020 KAKAO BLIND RECRUITMENT - 문자열 압축 (0) | 2021.05.01 |
[Programmers/JAVA] 2021 KAKAO BLIND RECRUITMENT - 신규 아이디 추천 (0) | 2021.05.01 |