-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj_4889.java
46 lines (38 loc) · 1.24 KB
/
boj_4889.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
44
45
46
package greedy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class boj_4889 {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int num=0;
while(true){
String s=br.readLine();
if(s.contains("-")) break;
num++;
char[] arr=s.toCharArray();
int n=arr.length;
//안정적인 문자열로 바꾸는데 필요한 최소 연산의 수 저장
int count=0;
Stack<Character> stack=new Stack<>();
for(int i=0;i<n;i++){
char cur=arr[i];
if(cur=='{'){
stack.push(cur);
}
else{ //'}' 인 경우
if(stack.isEmpty()){
count++;
stack.push('{');
}else{
stack.pop();
}
}
}
sb.append(num+". "+(count+stack.size()/2)).append("\n");
}
System.out.println(sb);
}
}