-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNestingDepth.java
107 lines (106 loc) · 2.37 KB
/
NestingDepth.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.*;
import java.io.*;
public class NestingDepth
{
public static void main(String[] args)
{
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int t = sc.nextInt();
for (int i = 1; i <= t; ++i)
{
String s=sc.next();
String ans="";
String r="";
for(int x=0;x<s.length();x++)
{
char ch=s.charAt(x);
if(ch=='0')
{
if(r.length()!=0)
{
ans=ans+solve(r,0);
}
ans=ans+"0";
r="";
}
else
{
r=r+ch;
}
}
if(r.length()!=0)
{
ans=ans+solve(r,0);
}
System.out.println("Case #"+i+": "+ans);
}
}
/*public static String solve(String s)
{
String ans="";
String r="";
for(int x=0;x<s.length();x++)
{
char ch=s.charAt(x);
if(ch=='0')
{
if(r.length()!=0)
{
ans=ans+solve(r,0);
}
ans=ans+"0";
r="";
}
else
{
r=r+ch;
}
}
if(r.length()!=0)
{
ans=ans+solve(r);
}
}*/
public static String solve(String s,int c)
{
String ans="";
char min='0';
for(char x='1';x<='9';x++)
{
if(s.indexOf(x)!=-1)
{
min=x;
break;
}
}
String r="";
int d=min-48-c;
//System.out.println(d);
for(int x=0;x<s.length();x++)
{
char ch=s.charAt(x);
if(ch==min)
{
if(r.length()!=0)
{
ans=ans+solve(r,c+d);
}
ans=ans+min+"";
r="";
}
else
{
r=r+ch;
}
}
if(r.length()!=0)
{
ans=ans+solve(r,c+d);
}
for(int x=0;x<d;x++)
{
ans="("+ans+")";
}
return ans;
}
}