[Henan Province ACM-fifth session] trial products (Nyoj 542)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.