# 1179 Chemical Equation – PAT甲级真题

chemical equation is the symbolic representation of a chemical reaction in the form of symbols and formulae, wherein the reactant entities are given on the left-hand side and the product entities on the right-hand side. For example, CH4​+2O2​=CO2​+2H2​O means that the reactants in this chemical reaction are methane and oxygen: CH4​ and O2​, and the products of this reaction are carbon dioxide and water: CO2​ and H2​O.

Given a set of reactants and products, you are supposed to tell that in which way we can obtain these products, provided that each reactant can be used only once. For the sake of simplicity, we will consider all the entities on the right-hand side of the equation as one single product.

### Input Specification:

Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤20), followed by N distinct indices of reactants. The second line gives an integer M (1≤M≤10), followed by M distinct indices of products. The index of an entity is a 2-digit number.

Then a positive integer K (≤50) is given, followed by K lines of equations, in the format:

reactant_1 + reactant_2 + … + reactant_n -> product

where all the reactants are distinct and are in increasing order of their indices.

Note: It is guaranteed that

• one set of reactants will not produce two or more different products, i.e. situation like `01 + 02 -> 03` and `01 + 02 -> 04` is impossible;
• a reactant cannot be its product unless it is the only one on the left-hand side, i.e. `01 -> 01` is always true (no matter the equation is given or not), but `01 + 02 -> 01` is impossible; and
• there are never more than 5 different ways of obtaining a product given in the equations list.

### Output Specification:

For each case, print the equations that use the given reactants to obtain all the given products. Note that each reactant can be used only once.

Each equation occupies a line, in the same format as we see in the inputs. The equations must be print in the same order as the products given in the input. For each product in order, if the solution is not unique, always print the one with the smallest sequence of reactants — A sequence { a1​,⋯,am​ } is said to be smaller than another sequence { b1​,⋯,bn​ } if there exists 1≤imin(m,n) so that aj​=bj​ for all j<i, and ai​<bi​.

It is guaranteed that at least one solution exists.

### Sample Input:

8 09 05 03 04 02 01 16 10
3 08 03 04
6
03 + 09 -> 08
02 + 08 -> 04
02 + 04 -> 03
01 + 05 -> 03
01 + 09 + 16 -> 03
02 + 03 + 05 -> 08

### Sample Output:

02 + 03 + 05 -> 08
01 + 09 + 16 -> 03
04 -> 04

❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼

❤ 点击这里 -> 订阅《从放弃C语言到使用C++刷算法的简明教程》by 柳婼

❤ 点击这里 -> 订阅PAT甲级乙级、蓝桥杯、GPLT天梯赛、LeetCode题解离线版