[ECNU3510] burn it, roommate! (simulation)

Click here to see the problem surface

Roughly meaning of the questions: to give you a containing only \ (C, H, O \ ) formula, need to ask a few \ (mol \) of oxygen to burn completely to \ (CO_2 \) and \ (H_2O \) .

Analog +Chemistry

First, we determined the analog formulas (C, H, O \) \ number (This part of your own thinking)。

Then, suppose we have \ (C \) a \ (C \) , \ (H \) a \ (H \) , \ (O \) a \ (O \) .

Then we need the co \ (O \) number is \ (2C + \ H2 of FRAC = \ FRAC {H} + 4C 2 \) , subtracting the original \ (O \) is \ (\ frac {4C + 2O} 2-H \) .

The amount of oxygen then we need this formula is divided by two, i.e., \ (\ {FRAC-4C + H 2O}. 4 \) .

Code

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 100000
using namespace std;
int n,T=1;string s;
struct element
{
    int C,H,O;I element(CI x=0,CI y=0,CI z=0):C(x),H(y),O(z){}
    I void operator *= (CI x) {C*=x,H*=x,O*=x;}
    I void operator += (Con element& o) {C+=o.C,H+=o.H,O+=o.O;}
}S[N+5];
int main()
{
    RI i,x=0,fg=0;char lst;for(cin>>s,s+="P",n=s.length(),i=0;i^n;++i)//模拟,求出C,H,O的数量
    {
        if(isdigit(s[i])) {x=(x<<3)+(x<<1)+(s[i]&15);continue;}if(fg) S[T]*=x,S[T-1]+=S[T],--T,fg=x=0;
        else lst=='C'&&(S[T].C+=x?x:1),lst=='H'&&(S[T].H+=x?x:1),lst=='O'&&(S[T].O+=x?x:1),lst=x=0;
        if(s[i]=='(') {x&&(S[T]*=x,x=0,fg&&(S[T-1]+=S[T],--T)),S[++T]=element();continue;}
        if(s[i]==')') {i^n&&isdigit(s[i+1])?fg=1:(S[T-1]+=S[T],--T);continue;}lst=s[i]; 
    }
    W(T>1) S[T-1]+=S[T],--T;long long t=4LL*S[1].C+S[1].H-2LL*S[1].O;//计算
    t%2?printf("%lld/4",t):(t%4?printf("%lld/2",t/2):printf("%lld/1",t/4));return 0;//约分后输出
}

Guess you like

Origin www.cnblogs.com/chenxiaoran666/p/ECNU3510.html