2605. Molecular Weight Calculator

单点时限: 20.0 sec

内存限制: 256 MB

Even though not good at memorizing, Alex is fond of Chemistry. When solving problems, he always needs to calculate the

molecular weight and quantity of each kind of elements. Lazy Alex hates that and turns to you for help.

Your task is:

Given a chemical formula, calculate its molecular weight and quantity of each kind of elements.

The atom is represented by either a single capital letter or a capital letter and a lower case letter. E.g. C, Ca, H, O. Formulas

may contain brackets: ‘(‘,’)’, which is always followed by the quantity of that atomic mission. The number after an atom or a

bracket represents the quantity of that atomic mission. E.g. CH4, CaCO3, Fe(SCN)3. Brackets may be nested.


The input consists of multiple test cases. The first line of input contains an integer T, which is the number of test cases.

Each test case is on several lines. The first line contains an integer, N, indicating the quantity of known elements.

Each of the following n lines is consisting of a element description, start with a element name and followed by the weight

W (floating point number), separated by a space.

The (n+1)-th line consists an integer M, indicating the quantity of formulas.

Each of the following M lines is consisting of a formula.

[Technical Specification]

1.T is an integer, and T <=10.

2.N is an integer, and 1 <= N <= 200.

3.W is a floating point number with two fractional digits, and 1 <= W <= 400.

4.M is an integer, and 1 <= M <= 100.

5.Numbers in the formula is always in the range of [2,9] inclusive.

6.The formula?s length does not exceed 500.

7.There’s no blank line between two test cases.


For each formula in each test case, print a single line as the format below.


Weight Element[quantity] Element[quantity] Element[quantity] …

e.g. 44.00 C[1] O[2]

The element is ordered as they were in the input. Display all the elements except for those not in the formula. The weight is

rounded to two fractional digits. Notice that the answer may be very large.

There’s no whitespace at the end of each line.


H 1.00
C 12.00
O 16.00
N 14.00
S 32.00
Ca 40.00
Fe 56.00
44.00 C[1] O[2]
16.00 H[4] C[1]
100.00 C[1] O[3] Ca[1]
230.00 C[3] N[3] S[3] Fe[1]

0 人解决,1 人已尝试。

0 份提交通过,共有 1 份提交。

9.9 EMB 奖励。

创建: 11 年,8 月前.

修改: 3 年,3 月前.

最后提交: 11 年,7 月前.

来源: The 4th Baidu Cup Central China Invitational Programming Contest