[SWEA] 1218 괄호 짝짓기

Time Lapse :3min 2sec

1218.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;

public class Solution {
public static LinkedList<Integer> ll = new LinkedList<>();
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = 10, tc = 1;
while(T-- != 0){
bf.readLine();
bw.write("#" + tc++ + " " + result(bf.readLine()) + "\n");
}
bw.flush();
}
public static int result(String str) {
ll.clear();
switch (str.charAt(0)) {
case ')':
case ']':
case '}':
case '>': return 0;
default: break;
}
for(int i = 0; i < str.length(); ++i) {
switch (str.charAt(i)) {
case '(' : ll.push(1); break;
case '[' : ll.push(2); break;
case '{' : ll.push(3); break;
case '<' : ll.push(4); break;
case ')' : if(ll.isEmpty() || ll.pop() != 1) return 0; break;
case ']' : if(ll.isEmpty() || ll.pop() != 2) return 0; break;
case '}' : if(ll.isEmpty() || ll.pop() != 3) return 0; break;
case '>' : if(ll.isEmpty() || ll.pop() != 4) return 0; break;
default: break;
}
}
return 1;
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/04/PS/SWEA/1218/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.