-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnestingdepth.php
59 lines (58 loc) · 1.25 KB
/
nestingdepth.php
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
<?php
$handle = fopen ("php://stdin","r");
$nooftestcases = fgets($handle);
for($a=0;$a<$nooftestcases;$a++){
$number=str_split(trim(fgets($handle)));
$result=array();
$opencount=0;
$closecount=0;
for($i=0;$i<sizeof($number);$i++){
if($number[$i]!=0){
if($opencount!=$number[$i]){
if($number[$i]<$opencount){
// $counter=$opencoun-$number[$i];
for($j=$number[$i];$j<=$opencount;$j++)
{
array_push($result,')');
$opencount--;
}
array_push($result,$number[$i]);
}
else{
if($number[$i]-1==$opencount){
array_push($result,'(');
$opencount++;
}else
for($j=0;$j<$number[$i];$j++)
{
array_push($result,'(');
$opencount++;
}
array_push($result,$number[$i]);
}
}else{
array_push($result,$number[$i]);
}
}else{
for($j=$closecount;$j<$opencount;$j++){
array_push($result,')');
$closecount++;
}
$opencount=0;
$closecount=0;
array_push($result,$number[$i]);
}
}
if($opencount!=$closecount){
for($j=$closecount;$j<$opencount;$j++){
array_push($result,')');
$closecount++;
}
$opencount=0;
$closecount=0;
}
print "Case #".($a+1).": ".implode('',$result)."\n";
$result=array();
}
fclose($handle);
?>