forked from careercup/ctci
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion.java
More file actions
326 lines (306 loc) · 9.5 KB
/
Question.java
File metadata and controls
326 lines (306 loc) · 9.5 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package Question9_11;
import java.util.HashMap;
public class Question {
public enum Term {
True,
False,
And,
Or,
Xor,
LeftParen,
RightParen
};
public static String reduce(String expression, int start, int end) {
if (start == end) {
if (expression.charAt(start) == '1') {
return "1";
} else if (expression.charAt(start) == '0') {
return "0";
}
}
int count = 0;
int i = 0;
String[] reduced = new String[3];
int index = 0;
int left = start;
int right = start;
for (i = start; i <= end; i++) {
if (expression.charAt(i) == '(') {
if (count == 0) {
left = i + 1;
}
count++;
} else if (expression.charAt(i) == ')') {
count--;
if (count == 0) {
right = i - 1;
}
}
if (count == 0) {
reduced[index] = reduce(expression, left, right);
if (index == 0) {
reduced[index + 1] = Character.toString(expression.charAt(i + 1));
i += 1;
left = i + 1;
right = i + 1;
}
index += 2;
}
}
if (reduced[1].equals("&")) {
if (reduced[0].equals("1") && reduced[2].equals("1")) {
return "1";
}
return "0";
} else if (reduced[1].equals("|")) {
if (reduced[0].equals("1") || reduced[2].equals("1")) {
return "1";
}
return "0";
} else if (reduced[1].equals("^")) {
if (reduced[0].equals("1") && reduced[2].equals("0")) {
return "1";
} else if (reduced[0].equals("0") && reduced[2].equals("1")) {
return "1";
}
return "0";
}
return "0";
}
public static boolean evaluate(String expression, int start, int end) {
String result = reduce(expression, start, end);
if (result == "0") {
return false;
} else {
return true;
}
}
public static boolean isOperator(char c) {
switch (c) {
case '&':
case '|':
case '^':
return true;
default:
return false;
}
}
public static String insertParensAround(String expression, int ind) {
int left = 0;
int right = 0;
int index = 0;
int count = 0;
for (int i = 0; i < expression.length(); i++) {
if (isOperator(expression.charAt(i))) {
if (count == ind) {
index = i;
}
count++;
}
}
count = 0;
for (int i = index - 1; i >= 0; i--) {
if (expression.charAt(i) == ')') {
count++;
} else if (expression.charAt(i) == '(') {
count--;
}
if (count == 0) {
left = i;
break;
}
}
count = 0;
for (int i = index + 1; i <= expression.length(); i++) {
if (expression.charAt(i) == '(') {
count++;
} else if (expression.charAt(i) == ')') {
count--;
}
if (count == 0) {
right = i;
break;
}
}
if (left == 0 && right == expression.length() - 1) {
return expression;
}
String newexpression = expression.substring(0, left) + '(' + expression.substring(left, right + 1) + ')' + expression.substring(right + 1);
return newexpression;
}
public static int bruteForce(String expression, HashMap<String, Boolean> completed, boolean result, boolean[] flags) {
int count = 0;
boolean isDone = true;
if (completed.containsKey(expression)) {
return 0;
}
for (int i = 0; i < flags.length; i++) {
if (!flags[i]) {
flags[i] = true;
String newexpression = insertParensAround(expression, i);
isDone = false;
count += bruteForce(newexpression, completed, result, flags);
flags[i] = false;
}
}
if (isDone) {
if (evaluate(expression, 0, expression.length() - 1) == result) {
System.out.println(expression + " = " + result);
return 1;
} else {
System.out.println(expression + " = " + !result);
return 0;
}
}
completed.put(expression, true);
return count;
}
public static int countR(String exp, boolean result, int start, int end) {
if (start == end) {
if (exp.charAt(start) == '1' && result) {
return 1;
} else if (exp.charAt(start) == '0' && !result) {
return 1;
}
return 0;
}
int c = 0;
if (result) {
for (int i = start + 1; i <= end; i += 2) {
char op = exp.charAt(i);
if (op == '&') {
c += countR(exp, true, start, i - 1) * countR(exp, true, i + 1, end);
} else if (op == '|') {
c += countR(exp, true, start, i - 1) * countR(exp, false, i + 1, end);
c += countR(exp, false, start, i - 1) * countR(exp, true, i + 1, end);
c += countR(exp, true, start, i - 1) * countR(exp, true, i + 1, end);
} else if (op == '^') {
c += countR(exp, true, start, i - 1) * countR(exp, false, i + 1, end);
c += countR(exp, false, start, i - 1) * countR(exp, true, i + 1, end);
}
}
} else {
for (int i = start + 1; i <= end; i += 2) {
char op = exp.charAt(i);
if (op == '&') {
c += countR(exp, false, start, i - 1) * countR(exp, true, i + 1, end);
c += countR(exp, true, start, i - 1) * countR(exp, false, i + 1, end);
c += countR(exp, false, start, i - 1) * countR(exp, false, i + 1, end);
} else if (op == '|') {
c += countR(exp, false, start, i - 1) * countR(exp, false, i + 1, end);
} else if (op == '^') {
c += countR(exp, true, start, i - 1) * countR(exp, true, i + 1, end);
c += countR(exp, false, start, i - 1) * countR(exp, false, i + 1, end);
}
}
}
return c;
}
public static int countDP(String exp, boolean result, int start, int end, HashMap<String, Integer> cache) {
String key = "" + result + start + end;
if (cache.containsKey(key)) {
return cache.get(key);
}
if (start == end) {
if (exp.charAt(start) == '1' && result == true) {
return 1;
} else if (exp.charAt(start) == '0' && result == false) {
return 1;
}
return 0;
}
int count = 0;
if (result) {
for (int i = start + 1; i <= end; i += 2) {
char op = exp.charAt(i);
if (op == '&') {
count += countDP(exp, true, start, i - 1, cache) * countDP(exp, true, i + 1, end, cache);
} else if (op == '|') {
count += countDP(exp, true, start, i - 1, cache) * countDP(exp, false, i + 1, end, cache);
count += countDP(exp, false, start, i - 1, cache) * countDP(exp, true, i + 1, end, cache);
count += countDP(exp, true, start, i - 1, cache) * countDP(exp, true, i + 1, end, cache);
} else if (op == '^') {
count += countDP(exp, true, start, i - 1, cache) * countDP(exp, false, i + 1, end, cache);
count += countDP(exp, false, start, i - 1, cache) * countDP(exp, true, i + 1, end, cache);
}
}
} else {
for (int i = start + 1; i <= end; i += 2) {
char op = exp.charAt(i);
if (op == '&') {
count += countDP(exp, false, start, i - 1, cache) * countDP(exp, true, i + 1, end, cache);
count += countDP(exp, true, start, i - 1, cache) * countDP(exp, false, i + 1, end, cache);
count += countDP(exp, false, start, i - 1, cache) * countDP(exp, false, i + 1, end, cache);
} else if (op == '|') {
count += countDP(exp, false, start, i - 1, cache) * countDP(exp, false, i + 1, end, cache);
} else if (op == '^') {
count += countDP(exp, true, start, i - 1, cache) * countDP(exp, true, i + 1, end, cache);
count += countDP(exp, false, start, i - 1, cache) * countDP(exp, false, i + 1, end, cache);
}
}
}
cache.put(key, count);
return count;
}
public static int total(int n) {
// Function to return (2n) ! / ((n+1)! * n!)
// To keep the numbers small, we divide by i when possible to do evenly. If not,
// we store up the remainder and divide when possible.
long num = 1;
long rem = 1;
for (int i = 2; i <= n; i++) {
num *= (n + i);
rem *= i;
if (num % rem == 0) {
num /= rem;
rem = 1;
}
}
return (int)num;
}
public static int countDPEff(String exp, boolean result, int start, int end, HashMap<String, Integer> cache) {
String key = "" + start + end;
int count = 0;
if (!cache.containsKey(key)) {
if (start == end) {
if (exp.charAt(start) == '1') {
count = 1;
} else {
count = 0;
}
}
for (int i = start + 1; i <= end; i += 2) {
char op = exp.charAt(i);
if (op == '&') {
count += countDPEff(exp, true, start, i - 1, cache) * countDPEff(exp, true, i + 1, end, cache);
} else if (op == '|') {
int left_ops = (i - 1 - start) / 2; // parens on left
int right_ops = (end - i - 1) / 2; // parens on right
int total_ways = total(left_ops) * total(right_ops);
int total_false = countDPEff(exp, false, start, i - 1, cache) * countDPEff(exp, false, i + 1, end, cache);
count += total_ways - total_false;
} else if (op == '^') {
count += countDPEff(exp, true, start, i - 1, cache) * countDPEff(exp, false, i + 1, end, cache);
count += countDPEff(exp, false, start, i - 1, cache) * countDPEff(exp, true, i + 1, end, cache);
}
}
cache.put(key, count);
} else {
count = cache.get(key);
}
if (result) {
return count;
} else {
int num_ops = (end - start) / 2;
return total(num_ops) - count;
}
}
public static void main(String[] args) {
String terms = "0^0|1&1^1|0|1";
boolean result = true;
bruteForce(terms, new HashMap<String, Boolean>(), result, new boolean[(terms.length() - 1) / 2]);
System.out.println(countR(terms, result, 0, terms.length() - 1));
System.out.println(countDP(terms, result, 0, terms.length() - 1, new HashMap<String, Integer>()));
System.out.println(countDPEff(terms, result, 0, terms.length() - 1, new HashMap<String, Integer>()));
}
}