NYOJ542 trial products, <abuse simulation questions>, nyoj542 trial products
Trial product time limit: 1000 MS | memory limit: 65535 KB difficulty: 4
-
Description
-
Dr. Kong of ZZ University recently found that many trial products in the laboratory have been used up. Due to limited project funding, Dr. Kong decided to use existing laboratory trial products to generate the missing trial products. To this end, Dr. Kong spent several days working out a research paper and asked graduate student Bill to experiment and count the number of missing trial products.
After reading all the data from start to end, Bill found that the data was filled with a lot of chemical equations, except for uppercase and lowercase English letters, numbers, plus signs, and other numbers. Each equation isA1 +A2 + ...... +Ap=B1 +B2 + ...... +BqIndicates the trial product.A1,A2 ,...... AndApA test product is generated.B1,B2 ,......,Bq. WhereAI andBJ is a chemical formula of an element or compound (a length of no more than 10 characters), 1 ≤ p, q ≤ 20. The length of each equation cannot exceed 100 characters. The chemical formula of some test products may not be found in the periodic table of chemical elements in modern society, which is caused by physical reactions in the chemical reaction process.
Bill has a headache. From which experiment? Can you help him?
-
Input
-
There are multiple groups of test data.
The first line: N indicates the number of chemical equations written by Dr. Kong (1 <= N <= 400)
Next there are N rows, and each row is an equation.
Next line: M indicates the number of existing test products. (1 <= M <= 500)
Next, there are M rows, and each row is an existing chemical formula for testing products.
-
Output
-
The first line contains a number T, indicating the number of missing products that can be generated.
In the next T line, the chemical formula of the test product is output in ascending ASCII code.
-
Sample Input
-
4H2O+Na=NaOH+H2Cl2+H2=HClFe+O2=Fe3O4NaOH+HCl=H2O+NaCl3H2ONaCl2
-
Sample output
-
4H2HClNaClNaOH
-
Source
-
Fifth Henan Program Design Competition
-
Uploaded
ACM _ Li rubing
Ideas:
Since the data is very small, we can traverse the equation infinitely. We put the existing substances into the map, break down the equation, and let us determine whether the equation has occurred. When this happens, we put the generated substances into the map, if a new equation does not exist, it means it will not happen again.
# Include <bits/stdc ++. h> using namespace std; struct fcs {string sz; bool fs; // equation with fs mark occurrence no} a [444]; int main () {int n, m, i, j; while (scanf ("% d", & n) = 1) {for (I = 0; I <n; ++ I) {cin> a [I]. sz; a [I]. fs = false;} scanf ("% d", & m); map <string, bool> jb; // use map to indicate whether the Bool at the right of an existing substance indicates the product or the reaction for (I = 0; I <m; ++ I) {string s; cin> s; jb [s] = false;} bool sad = true; // mark the variable when determining whether an equation has occurred while (sad) {sad = false; for (I = 0; I <n; ++ I) {if (a [I]. f S) // if this equation has occurred, it will not look at continue; int len, gfs = 0; string wz; // The decomposition equation is used to obtain a single substance for (j = 0, len = a [I]. sz. size (); j <len; ++ j) {if (a [I]. sz [j] = '+' | a [I]. sz [j] = ') // + or equal to the end {if (! Jb. count (wz) // indicates that the substance does not exist and directly exits break; wz = ""; if (a [I]. sz [j] = ') // when the equal sign is used, it indicates that all substances have this equation. {a [I]. fs = true; // mark this equation as when sad = true; // This wheel has a substance to generate gfs = 1; // mark break;} continue ;} wz + = a [I]. sz [j]; // connect // if (a [I]. sz [j]! = '+' & A [I]. sz [j]! = ')} If (gfs) // Add the product to {wz = ""; for (++ j; j <len; ++ j) // {if (a [I]. sz [j] = '+') {if (jb. count (wz) // determine {wz = ""; continue;} jb [wz] = true; // insert wz = ""; continue ;} wz + = a [I]. sz [j];} if (! Jb. count (wz) // The Last One must be heavy. For the first time, the jb [wz] = true ;}} cout <(jb. size ()-m) <endl; map <string, bool>: iterator p = jb. begin (); while (p! = Jb. end () // output {if (p-> second) cout <p-> first <endl; p ++ ;}} return 0 ;}
I wrote this question for two days in my freshman year, and my heart was broken. I will try again in a year. It feels pretty good.