当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

2297:Balanced Chemical Equations

题目描述
One cumbersome problem in chemistry is the task of making the number of atoms balanced in a chemical equation. Our problem is concerned with this.

Chemists obey these rules when they present chemical equations:
  1. Each element name is abbreviated by at most two letters. The first letter is always in upper-case and the second letter if exists, is a lower-case letter (e.g. Calcium is represented by Ca, Oxygen by O, and Chlorine by Cl).
  2. Each molecule is composed of a number of atoms. To represent a molecule, we concatenate the abbreviated names of its composite atoms. For example, NaCl represents Sodium Chloride. Each atom name may be followed by a frequency number. For example, Calcium Chloride CaCl2 consists of one atom of Calcium and two atoms of Chlorine. If the frequency is not given, it is assumed to be 1 (so HCl is equivalent to H1Cl1). For the sake of simplicity, you may assume that the frequency of an occurrence of an atom is at most 9 (so we do not have C11H22O11 in the problem input). Note that there may be several occurrences of the same atom in the molecule formula, like H atom in CH3COOH.
  3. In ordinary chemical reactions, a number of molecules combine and result in a number of other molecules. For example a well known sample of neutralization is:
    2HCl + CaO2H2 → CaCl2 + 2H2O

    This means two molecules of chlorohydric acid (HCl) with one molecule of Calcium Hydroxide results in one molecule of Calcium Chloride (CaCl2) and two molecules of water.
  4. In every chemical reaction, the total number of each atom in the right side of the equation equals the total number of that atom in the left side (that is why it is called an equation!).

Your task is to write a program to find appropriate coefficients x1 , x2 , …, xM and y1 , y2 , …, yN to balance an (imbalanced) equation like
A1+A2+A3+ ... +AM → B1+B2+B3+ ... +BN

in the following way:
x1 A1+ x2 A2+ x3 A3+ ... + xM AM → y1 B1+ y2 B2+ y3 B3+ ... + yN BN

输入解释
The first line contains an integer t (1 <= t <= 10), the number of test cases. Each test case consists of a single line containing an expression like:
A1+A2+A3+ ... +AM = B1+B2+B3+ ... +BN

Each Ai or Bi is a formula of a molecule according to the rules given in items 1 and 2.
The input equations are given in a way that if they can be balanced, xi and yi coefficients can be in the range of 1 to 9. There are less than 10 molecules and there are less than 10 different types of atoms, in a given equation. Additionally, you may assume molecules contain no more than 3 different kinds of atoms. You may also assume that there is no blank character in the input file, and the maximum length of the input lines is 200 characters.
输出解释
The output will be one line per test case containing the list of required coefficients, separated by blank characters, in the following order:
x1 x2 ... xM y1 y2 ... yN

The coefficients should be integers in the range of 1..9. Obviously, there may be more than one answer for a test case. In such situations, print the answer which minimizes the number:
x1x2...xMy1y2...yN

(This is an (M+N)-digit decimal number whose digits are xi and yi coefficients.) If the equation is impossible to balance, the output line should be IMPOSSIBLE.
输入样例
3
HCl+CaO2H2=CaCl2+H2O
HCl+H2SO4=NaCl
HCl+NaOH=NaCl+H2O
输出样例
2 1 1 2
IMPOSSIBLE
1 1 1 1

该题目是Virtual Judge题目,来自 北京大学POJ

题目来源 Tehran 2003

源链接: POJ-2297

最后修改于 2020-10-29T06:28:59+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
1000 65536