-
Notifications
You must be signed in to change notification settings - Fork 8
/
b_nesting_depth.java
48 lines (44 loc) · 1.22 KB
/
b_nesting_depth.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
47
48
import java.util.*;
import java.io.*;
import java.lang.Math;
public class Solution {
public static void main (String[] args) {
boolean debug = false;
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
String tString = in.nextLine();
int t = Integer.parseInt(tString);
for (int i = 1; i <= t; ++i) {
String string = in.nextLine();
char[] cArr = string.toCharArray();
int currentStack = 0;
String answer = "";
for (char currentChar: cArr) {
int currentInt = Character.getNumericValue(currentChar);
if (currentInt > currentStack) {
while (currentInt > currentStack) {
answer += "(";
currentStack++;
}
answer += Integer.toString(currentInt);
} else if (currentInt == currentStack) {
answer += currentInt;
} else if (currentInt < currentStack) {
while (currentInt < currentStack) {
answer += ")";
currentStack--;
}
answer += Integer.toString(currentInt);
}
}
while (currentStack > 0) {
answer += ")";
currentStack--;
}
if (debug) {
System.out.println("Case #" + i + ": " + Arrays.toString(cArr));
System.out.println(answer);
}
System.out.println("Case #" + i + ": " + answer);
}
}
}