Title:Trial Products
time limit: + MS | Memory limit:65535 KB
Difficulty: 4
Describe
The Dr.kong of ZZ University recently found that many prototypes of laboratories have been exhausted. Due to the limited funding of the project, Dr.kong decided to use the existing prototype in the laboratory to produce the missing samples. To this end, Dr.kong a few days a day to finish out a research material and let graduate bill to experiment and statistics can produce how many kinds of missing test products.
Bill went through all the information, found that the material is filled with a lot of chemical equations, in addition to uppercase and lowercase letters, numbers, plus, equal sign, there is no other symbol. Each of these equations isA1+A2+......+AP=B1+B2+......+BQthe form of a prototype productA1,A2,...... and theAPreaction, produced a prototype productB1,B2,......,BQ. WhichAIand theBJis a chemical formula of a simple substance or compound (not exceeding 10 characters in length), 1≤p,q≤20. The total length of each equation is no more than 100 characters. The chemical formula of some products may not be found in the periodic table of chemical elements in modern society, as a result of possible physical reactions during chemical reactions.
Bill has a headache, which experiment to start with? Can you help him?
Input
There are multiple sets of test data.
First line: N indicates the number of chemical equations written by Dr.kong (1<=n<=400)
Next there are n rows, each line being an equation.
The next line: M indicates how many kinds of products have been tried. (1<=m<=500)
Next there are m lines, each of which is the chemical formula of a test product already in place.
Output
The first line contains a number T, which indicates how many of the missing samples can be produced.
In the next T-line, the chemical formula for the resulting sample is output in ascending ASCII code.
Sample input
4
H2o+na=naoh+h2
Cl2+h2=hcl
Fe+o2=fe3o4
Naoh+hcl=h2o+nacl
3
H2o
Na
Cl2
Sample output
4
H2
HCl
Nacl
NaOH
Source
Fifth session of Henan Province Program design Competition
Main topic:give an M chemical equation, n items, how many other items can be synthesizedTopic Ideas:
1. Decomposition of chemical equations into individual items2. Use map to convert items into numbers. Some to record3, circular search equations, once new items are added, you have to traverse one side.
4. Map starts to traverse, if there is no output for the original
Program:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath > #include <algorithm> #include <cctype> #include <fstream> #include <limits> #include < vector> #include <list> #include <set> #include <map> #include <queue> #include <stack> #include <cassert>using namespace Std;bool fv[3000];//originally given the bool v[30000];//element there is no bool vv[500];//equation set not int m,n; The struct formula{int in[30];//equation requires an int out[30];//equation output int in_len; int Out_len;}; int main () {while (~SCANF ("%d", &m)) {formula a[500]; map<string,int>ma; int num=1; string S; for (int i=0; i<m; i++) {cin>>s; int Len=s.length (); int w=0,f=0; for (int j=0; j<len; J + +) if (! ( s[j]== ' + ' | | s[j]== ')} {if (s[j]== ' = ') {f=1; w=0; Continue } int k; for (k=j+1; k<=len; k++) if (s[k]== ' + ' | | s[k]== ' | | s[k]== ' = ') break; String SS (S,j,k-j);//Extract the item//cout<<f<<ss<< ' \ t '; if (!ma[ss]) ma[ss]=num++; if (!f) A[I].IN[W++]=MA[SS]; else A[I].OUT[W++]=MA[SS]; if (!f) a[i].in_len=w; else a[i].out_len=w; J=k-1; }//cout<<endl; } scanf ("%d", &n); memset (Fv,0,sizeof (FV)); memset (v,0,sizeof (v)); memset (vv,0,sizeof (vv)); for (int i=0; i<n; i++)//Record the item {cin>>s; if (Ma[s]) {v[ma[s]]=1; Fv[ma[s]]=1; }} int f=1;//update on the loop while (f) {f=0; for (int i=0; i<m; i++) if (!vv[i]) {int J; for (j=0; j<a[i].in_len; j + +) if (!v[a[i].in[j]) goto en;//Insufficient chemical reaction for (j=0; j<a[i].out_len; j + +) V[a[i].out[j]]=1; f=1; Vv[i]=1;en:; }} int ans=0; Map<string, Int>::iterator it;//traverse for (It=ma.begin (); It!=ma.end (); it++) if (v[it->second]& &!fv[it->second]) ans++; cout<<ans<<endl; For (It=ma.begin (); It!=ma.end (); it++) if (V[it->second]&&!fv[it->second]) cout< <it->first<<endl; } return 0;}
Nyoj 542 Trial Products