|
||||||||||
Molecular Weight CalculatorTime Limit: 60000/20000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 184 Accepted Submission(s): 21 Problem Description 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. Input 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. Output For each formula in each test case, print a single line as the format below. Format: 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. Sample Input
Sample Output
Source | ||||||||||
|