-
Notifications
You must be signed in to change notification settings - Fork 13
/
equivalent-resistance-circuit-building.java
50 lines (47 loc) · 1.87 KB
/
equivalent-resistance-circuit-building.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
// Code adapted from the user "[4G]" (mine did not use regEx)
import java.util.*;
import java.util.regex.*;
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
Pattern p1 = Pattern.compile("(\\([^()\\[\\]]+\\)|\\[[^()\\[\\]]+\\])");
Pattern p2 = Pattern.compile("(\\w+)");
Map<String, int[]> map = new HashMap<>();
String p = "";
int gen = 1001;
int N = in.nextInt();
for (int i = 0; i < N; i++) { map.put(in.next(), new int[]{in.nextInt(), 1}); }
in.nextLine();
String c = in.nextLine();
while(!p.equals(c)) {
p = c;
Matcher m1 = p1.matcher(c);
while(m1.find()) {
boolean s = m1.group(1).startsWith("(");
Matcher sub = p2.matcher(m1.group(1));
String ncode = "HSH" + gen++;
int[] nV = {s?0:1, s?1:0};
while(sub.find()) {
int[] pV = map.get(sub.group(1));
if ( s ) {
nV[0] = nV[0] * pV[1] + nV[1] * pV[0];
nV[1] *= pV[1];
}
else {
nV[1] = nV[1] * pV[0] + nV[0] * pV[1];
nV[0] *= pV[0];
}
}
int d1 = nV[0], d2 = nV[1];
while(d2 > 0) {
int tmp = d1 % d2;
d1 = d2;
d2 = tmp;
}
map.put(ncode, new int[] {nV[0]/d1, nV[1]/d1});
c = c.replaceFirst(m1.group(1).replaceAll("([()\\[\\]])", "\\\\$1"), ncode);
}
}
System.out.println(String.format("%.1f", 1.0 * map.get(c)[0] / map.get(c)[1]));
}
}