Compute the equivalent resistance of a nested series-parallel resistor circuit described by a bracketed string.
Easy3StackMathNo attempts yetTime limit1sMemory limit64 MBMirko got a "Young physicist" kit for his birthday. The kit holds N types of resistors, connecting wires, and an ohmmeter. A resistor of type i has a resistance of exactly Ri ohms.
Resistors are connected in two ways, in series and in parallel. A whole circuit of resistors can be replaced by a single resistor whose resistance equals the equivalent resistance of that circuit. A series circuit is written as (R1-R2-R3-...-RK), and its equivalent resistance is
Req=R1+R2+R3+⋯+RK
A parallel circuit is written as (R1|R2|R3|...|RK), and its equivalent resistance is
Req=R11+R21+R31+⋯+RK11
Mirko brought the kit to school and the teacher noticed it. She built one circuit and is asking Mirko for its equivalent resistance. Help him answer.
The first line contains the number of resistor types N. (1≤N≤9)
The second line contains N real numbers Ri. (0<Ri<100)
The third line contains the string S describing the circuit. S is at most 100,000 characters long and consists only of the characters R, the digits 1 to 9, (, ), -, and |. The number of bracket pairs is minimal and the brackets are paired correctly. No pair of brackets contains both - and |. The circuit uses only resistors from the kit. Some types may go unused, and a type may be used several times.
Print the equivalent resistance of the circuit, rounded to exactly five decimal places. Pad with zeros so that five digits always follow the decimal point.