Simulation
A collection of reactants; b generating Set; c The presence of a compound or elemental set; Ans New Generation compounds collection
1, if the reaction is not all in the generated compound set, then complete the reaction, the composition is added to the C set
2. Sort the reactants of each equation to facilitate faster search
3, keep searching until no new compounds are generated.
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<Set>using namespacestd;#defineN 402Vector<string>a[N];//left-side compound setvector<string>b[N];//right-type compound setvector<string>c;//presence of compoundsSet<string>ans;//newly generated compoundsBOOLFound[n];//whether the marker chemistry has been synthesizedintMain () {Freopen ("D:\\in.txt","R", stdin); intN, M; while(cin>>N) {memset (found,false,sizeof(found)); for(intI=0; i<n; i++) {a[i].clear (); B[i].clear (); } c.clear (); Ans.clear (); stringexpr, str; for(intI=0; i<n; i++) {cin>>expr; intMID = Expr.find ("=",0); Expr[mid]='+'; Expr+="+"; intLen =expr.size (); for(intj =0; J<mid+1; J + +){ if(Expr[j]! ='+') Str.push_back (Expr[j]); Else{a[i].push_back (str); Str.clear (); } } for(intj = mid+1; j<len; J + +){ if(Expr[j]! ='+') Str.push_back (Expr[j]); Else{b[i].push_back (str); Str.clear (); } }} cin>>m; for(intI=0; i<m; i++) {cin>>expr; C.push_back (expr); } for(intI=0; i<n; i++) {//sort Easy to findsort (A[i].begin (), A[i].end ()); } BOOLHasnext =true; while(hasnext) {Hasnext=false; for(intk=0; k<n; k++){ if(Found[k])Continue; stringFirst = a[k][0]; if(Find (C.begin (), C.end (), first)! = C.end ()) {//if the first element of an equation exists intSize =a[k].size (); inti =1; for(; i<size; i++) {//find chemical residue compounds in existing compound set if(Find (C.begin () +1, C.end (), a[k][i]) = =c.end ()) Break; } if(i = = size) {//if the left-hand exact matchFOUND[K] =true; Hasnext=true; for(vector<string>::iterator it = B[k].begin (); It! = B[k].end (); it++){ if(Find (C.begin (), C.end (), *it) = =C.end ()) {C.push_back (*it); Ans.insert (*it); } }}}} cout<<ans.size () <<Endl; for(Set<string>::iterator it = Ans.begin (); It! = Ans.end (); it++) cout<<*it<<Endl; } return 0;}
[Henan Province ACM-fifth session] trial products (Nyoj 542)