https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14eWb6AAkCFAYD
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Solution {
public static boolean mapping (char c, char top) {
if ((c == ')' && top == '(') || (c == ']' && top == '[') || (c == '}' && top == '{') || (c == '>' && top == '<'))
return true;
else
return false;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
int len = Integer.parseInt(br.readLine());
String str = br.readLine();
int result = 1; // 1:유효, 2:유효하지 않음
Stack<Character> stack = new Stack<>();
for (int i= 0; i < len; i++) {
char c = str.charAt(i);
switch(c) {
case '(' :
case '[' :
case '{' :
case '<' :
stack.push(c);
break;
default :
if (!stack.empty() && mapping(c, stack.peek()))
stack.pop();
else
result = 0;
}
if (result == 0)
break;
}
System.out.println("#" + t + " " + result);
}
}
}
'Algorithm > 백준+프로그래머스+SWEA+정올+구름' 카테고리의 다른 글
[Algorithm] SWEA 1940 가랏! RC카! (0) | 2021.08.05 |
---|---|
[Algorithm] SWEA 1225 암호생성기 (0) | 2021.08.05 |
[Algorithm] 백준 2493 탑 (0) | 2021.08.05 |
[Algorithm] 백준 5622 다이얼 (0) | 2021.08.04 |
[Algorithm] SWEA 2001 파리 퇴치 (0) | 2021.08.04 |