-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMobilesAlabama.java
More file actions
223 lines (159 loc) · 3.88 KB
/
Copy pathMobilesAlabama.java
File metadata and controls
223 lines (159 loc) · 3.88 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.Scanner;
public class MobilesAlabama {
final static String MATCH_BEFORE_OR_AFTER = "((?<=%1$s)|(?=%1$s))";
// Lookaround splitting delimiter borrowed from Godmar Back's example code
// match whitespace or right before or after ( )
// consumes whitespace - but does not consume ( or )
final static String delim = "\\p{javaWhitespace}|"
+ String.format(MATCH_BEFORE_OR_AFTER, "[\\(\\)]");
Deque<String> tokens = new ArrayDeque<String>();
ArrayList<String> ans = new ArrayList<String>();
Scanner in = new Scanner(System.in).useDelimiter(delim);
// Grammar:
// 1. ( D w )
// D indiciates DecObject
// w is weight
// 2. ( B # L m1 m2 )
// B indicates Bar
// # is identifier
// L is length
// m1 is expression 1
// m2 is expression 2
public void solve() {
while (inputExpression() > 2) {
poll(); // Removes first (
poll(); // Removes first B
Bar topBar = (Bar) parseBar();
ans.addAll(topBar.balance());
Collections.sort(ans, new AnswerComparator());
// Output answers
for (String s : ans) {
System.out.println(s);
}
ans.clear();
}
in.close();
}
char peek() {
if (tokens.isEmpty()) {
return '0';
}
return tokens.peek().charAt(0);
}
char poll() {
char ans = peek();
tokens.poll();
return ans;
}
// Assumes given the input tokens " # L (...) (...) )"
Node parseBar() {
int barNum = Integer.parseInt(tokens.poll());
double length = Double.parseDouble(tokens.poll());
poll(); // Remove initial (
Node m1;
Node m2;
char next = poll();
if (next == 'D') {
m1 = parseD();
} else {
m1 = parseBar();
}
poll(); // Remove ( prefix
next = poll();
if (next == 'D') {
m2 = parseD();
} else {
m2 = parseBar();
}
poll(); // Remove trailing )
return new Bar(barNum, length, m1, m2);
}
// Assumes given "w )"
Node parseD() {
Node ans = new DecObject(Double.parseDouble(tokens.poll()));
poll(); // Remove trailing )
return ans;
}
int inputExpression() {
tokens.clear();
int numOpen = 1;
String next = in.next();
while (next.equals("")) {
next = in.next();
}
tokens.add(next);
while (numOpen != 0) {
next = in.next();
if (next.equals("")) {
continue;
} else if (next.equals("(")) {
numOpen++;
} else if (next.equals(")")) {
numOpen--;
}
tokens.add(next);
}
return tokens.size();
}
static interface Node {
double weight();
}
static class DecObject implements Node {
double weight;
public DecObject(double weight) {
this.weight = weight;
}
public double weight() {
return weight;
}
}
static class Bar implements Node {
int barNum;
double length;
Node m1;
Node m2;
public Bar(int num, double l, Node m1, Node m2) {
barNum = num;
length = l;
this.m1 = m1;
this.m2 = m2;
}
public double weight() {
return m1.weight() + m2.weight();
}
public ArrayList<String> balance() {
ArrayList<String> ans = new ArrayList<String>();
if (m1 instanceof Bar) {
ans.addAll(((Bar) m1).balance());
}
if (m2 instanceof Bar) {
ans.addAll(((Bar) m2).balance());
}
double totalWeight = weight();
double L1 = (length * m1.weight()) / totalWeight;
double L2 = (length * m2.weight()) / totalWeight;
double best = Math.min(L1, L2);
ans.add(String.format("Bar %d must be tied %.1f from one end.",
barNum, best));
return ans;
}
}
public static void main(String[] args) {
new MobilesAlabama().solve();
}
class AnswerComparator implements Comparator<String> {
@Override
public int compare(String a1, String a2) {
String[] ans1 = a1.split(" ");
String[] ans2 = a2.split(" ");
int first = Integer.parseInt(ans1[1]);
int second = Integer.parseInt(ans2[1]);
return first - second;
}
}
}