NYOJ1236 Challenge Chamber of Secrets

Source: Internet
Author: User
Tags time limit
Challenge Chamber of SecretsTime limit: Ms | Memory limit: 65535 KB Difficulty: 4

Describe


The R-organized agent Dr. Kong was unfortunately caught in the WTO chamber of Secrets in search of the missing super-body element. Dr Kong must quickly find the unlocking code to flee, or a few minutes later, the WTO chamber of Secrets is about to explode.

Dr Kong found that the walls of the Chamber of Secrets were written in many chemical equations. A chemical equation, also known as a chemical reaction equation, is a chemical expression of a substance's chemical reaction. The chemical equation reflects an objective fact. Therefore, the formulation of chemical equations to abide by two principles: first, must be based on objective facts, the second is to abide by the law of mass conservation.

The chemical equation not only shows the reactants, the produce and the reaction conditions. At the same time, the stoichiometric number represents the quantity relationship between the reactants and the generated matter, which can also be expressed by the relative molecular mass or the relative atomic mass, which is the mass ratio between the substances. For gaseous reactants and generators, volume ratios can also be obtained directly from stoichiometric numbers. Example: 2NAOH+H2SO4=NA2SO4+2H2O

After many tests and inferences, Dr Kong found that the password is a 4-digit number, which is hidden in the first molecule after the chemical equation equals sign, and its molecular weight may be the password (if the molecular weight is less than 4 bits, preceded by 0).

Fortunately, Dr. Kong also remembers the atomic weight of the chemical elements used on the walls of the wall as follows:

N

C

O

Cl

S

H

Al

Ca

Zn

Na

14

12

16

35

32

2

27

40

65

23

Can you help Dr. Kong to find the password soon.


Input


The first line: K, indicating that there is a k chemical equation;

Then there's K-line, one chemical equation for each action.

Output


For each chemical equation output one line: That is, the password.


Sample input

3
2c+o2=2co2
Naoh+h2so4=na2so4+2h2o
CA2CO3+H2O=CA2 (OH) 2+co2

Sample output

0056
0142
0116


Tips

2≤k≤8, the chemical equation is not longer than 50, and the number of atoms and molecules does not exceed 9. The parentheses are at most one level.

Source

Eighth session of Henan Province Program design Competition

Uploaded by

Hnu_acm

I'm using an array simulation, and of course it can be done with stacks, and the expression evaluates to be similar #include <stdio.h> #include <string.h> #include <algorithm> #
Include<queue> using namespace std;
    int main () {int t;
    scanf ("%d", &t);
    GetChar ();
        while (t--) {int i,j,len,m,n;
        Char a[1005];
        Gets (a);
        Len=strlen (a);
        int flag=0,ans=0,ants,tlag=1,tt,slag=0,antt=0;
                for (i=0;i<len;i++) {if (a[i-1]== ' = ' &&a[i]>= ' 2 ' &&a[i]<= ' 9 ')//The case of a number after the equals sign
                tlag=a[i]-' 0 ';//indicates that there is a multiple of the number to multiply after the equal sign, and if not, the 1,tlag initial value is set to 1 if (flag==1)//Is the case after the equals sign {
                if (a[i]== ')//The opening parenthesis must first add up all the relative atomic mass in parentheses {slag=1;
                    if (a[i]== ' N ' &&a[i+1]== ' a ') {if (slag==1)//If an opening parenthesis is encountered, the following judgments are similar The antt+=23;//holds the molecular weight in parentheses, and the following judgments are similar to ants=23,ans+=ants,a[i+1]= ' 1 ';//ans holds the required molecular weight, ants stores the current added points
        The following are similar to the sub-quantity        } else if (a[i]== ' N ') {if (slag==1) antt+=1
                    4;
                ants=14,ans+=ants;
                    } else if (a[i]== ' C ' &&a[i+1]== ' l ') {if (slag==1)
                    antt+=35;
                ants=35,ans+=35,a[i+1]= ' 1 ';
                    } else if (a[i]== ' C ' &&a[i+1]== ' a ') {if (slag==1)
                    antt+=40;
                ants=40,ans+=40,a[i+1]= ' 1 ';
                    } else if (a[i]== ' C ') {if (slag==1) antt+=12;
                ants=12,ans+=12;
                    } else if (a[i]== ' O ') {if (slag==1) antt+=16;
                ants=16,ans+=16; } else if (a[i]== ' S ') {iF (slag==1) antt+=32;
                ants=32,ans+=32;
                    } else if (a[i]== ' H ') {if (slag==1) antt+=2;
                ants=2,ans+=2;
                    } else if (a[i]== ' a ' &&a[i+1]== ' l ') {if (slag==1)
                    antt+=27;
                ants=27,ans+=27,a[i+1]= ' 1 ';
                    } else if (a[i]== ' Z ' &&a[i+1]== ' n ') {if (slag==1)
                    antt+=65;
                ants=65,ans+=65,a[i+1]= ' 1 '; } if (a[i]>= ' 2 ' &&a[i]<= ' 9 ' &&i!=tt+1) {if (a[i-1]=
                    = ') ') ans+=antt* (a[i]-' 1 ');
                else ans+= (a[i]-' 1 ') *ants; } if (a[i]== ' + ' | |
                    a[i]== ' \ n ')//encountered ' + ' or end of string stop calculationflag=0;
        } if (a[i]== ' = ')//encounters an equals sign flag of 1, the Mark TT is to see the latter is not the number flag=1,tt=i;
    } printf ("%04d\n", Ans*tlag);
} return 0;
 }



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.