import
java.util.Scanner;
import
java.util.Stack;
class
Solution {
public
static
void
main(String args[])
throws
Exception {
Scanner sc =
new
Scanner(System.in);
int
[] pair =
new
int
[
256
];
// 아스키코드
pair[
'('
] =
')'
;
pair[
'['
] =
']'
;
pair[
'{'
] =
'}'
;
pair[
'<'
] =
'>'
;
for
(
int
T =
1
; T <=
10
; T++) {
Stack<Character> stack =
new
Stack();
boolean
possible =
true
;
// [input] Test 번호
int
inputLen = Integer.parseInt(sc.nextLine());
// [input] 데이터 입력 & 풀이
String line = sc.nextLine();
for
(
int
i =
0
; i < inputLen; i++) {
char
input = line.charAt(i);
// 괄호시작
if
(input ==
'('
||input ==
'['
||input ==
'{'
||input ==
'<'
)
{
stack.push(input);
}
// 괄호끝
if
(input ==
')'
||input ==
']'
||input ==
'}'
||input ==
'>'
)
{
if
(stack.empty()) {
possible =
false
;
break
;
}
if
(pair[stack.pop().charValue()] != input) {
possible =
false
;
break
;
}
}
}
System.out.println(String.format(
"#%d %d"
, T, possible?
1
:
0
));
}
}
}
'Knowledge > 알고리즘' 카테고리의 다른 글
1230. [S/W 문제해결 기본] 8일차 - 암호문3 (0) | 2017.09.30 |
---|---|
1224. [S/W 문제해결 기본] 6일차 - 계산기3 (0) | 2017.09.30 |
1216. [S/W 문제해결 기본] 3일차 - 회문2 (0) | 2017.09.30 |
1226. [S/W 문제해결 기본] 7일차 - 미로1 (0) | 2017.09.30 |
1208. [S/W 문제해결 기본] 1일차 - Flatten (0) | 2017.09.30 |