A 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+2H2O 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 H2O.
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≤i≤min(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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cctype>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <cstring>
#include <iomanip>
#include <sstream>
using namespace std;
#pragma warning(disable:4996)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define repr(i, a, b) for(int i = a; i >= (b); --i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
const int maxn = 10005;
const int INF = 0x3f3f3f3f;
int n, m;
struct formulate {
vector<int> reactant;
int product;
};
set<int> reactant;
vector<formulate> res, tmp;
vector<int> p;
map<int, bool> used;
map<int, vector<formulate> > mp;
bool cmp(formulate a, formulate b) {
return a.reactant < b.reactant; //vector降序排列。。
}
void dfs(int idx) { //第idx个产品
if (res.size() != 0)
return;
if (idx == m) {
res = tmp;
return;
}
int product = p[idx];
sort(mp[product].begin(), mp[product].end(), cmp);
for (int i = 0; i < mp[product].size(); i++)
{
map<int, bool> tmpMp = used;
bool flag = false;
for (int j = 0; j < mp[product][i].reactant.size(); j++) {
if (used[mp[product][i].reactant[j]] == true) {
flag = true;
used = tmpMp; //还原使用情况
break;
}
used[mp[product][i].reactant[j]] = true;
}
if (flag) //提前剪枝
continue;
tmp.push_back(mp[product][i]);
dfs(idx + 1);
//回溯
used = tmpMp;
tmp.pop_back();
}
}
int main() {
cin >> n;
rep(i, 0, n) {
int t;
cin >> t;
reactant.insert(t);
}
cin >> m;
rep(i, 0, m) {
int t;
cin >> t;
p.push_back(t);
if (reactant.count(t)) {
formulate b;
b.reactant.push_back(t);
b.product = t;
mp[t].push_back(b);
}
}
int nums;
cin >> nums;
getchar();
string line;
rep(i, 0, nums) {
getline(cin, line);
int pro = stoi(line.substr(sz(line) - 2));
formulate a;
a.product = pro;
rep(i, 0, sz(line)) {
if (isdigit(line[i])) {
a.reactant.push_back(stoi(line.substr(i, 2)));
i++;
}
if (line[i] == '-')
break;
}
mp[pro].push_back(a);
}
dfs(0);
rep(i, 0, m) {
rep(j, 0, res[i].reactant.size()) {
if (j != 0)
printf(" + ");
printf("%02d", res[i].reactant[j]);
}
printf(" -> ");
printf("%02d\n", res[i].product);
}
return 0;
}