hdu化学方程式。。TLE。。。

#include <IOSTREAM>//vs运行完全正确,且getline输入完美。另,从vs复制过来的代码自带颜色,赞一个!
#include <STRING>
#include <CCTYPE>
using namespace std;
int times[2][52] = {0};
int side;
int x,y;
string s;
string::iterator i;
void count(char c,int add); 
int main()
{
      while(getline(cin,s))
      {
           side = 0;
           for (int j=0;j<2;j++)
           for (int k=0;k<52;k++)
                times[j][k] = 0;
           i = s.begin();
           x = y = 1;
           while (i!=s.end())
           {
                if (isalpha(*i))                                        //字母
                {
                     count(*i,x);
                }
                else if (isdigit(*i)&&(i==s.begin()||!isalpha(*(i-1))))
                {
                    x = int(*i)-48;
                }
                else if (isdigit(*i)&&isalpha(*(i-1)))
                {
                    y = int(*i)-48;
                    count(*(i-1),(y-1)*x);
                    if (islower(*(i-1)))
                    {
                        count(*(i-2),(y-1)*x);
                    }
                }
                else if (*i=='+')
                {
                     x = 1;
                }
                else if (*i=='=')
                {
                     side = 1;
                     x = 1;
                }
                else if (*i=='(')
                {
                     ;
                }
                else if (*i==')')
                {
                if ((i+1)!=s.end()&&isdigit(*(i+1)))
                {
                     y = int(*(i+1))-48;
                     string::iterator back = i-1;
                     int mid_y = 1;
                     while (*back!='(' )
                     {
                         if (isalpha(*back))
                         {
                              count(*back,(y-1)*x);
                              back--;
                         }
                         else if (isdigit(*back))
                         {
                              mid_y = int(*back)-48;
                              back-=2;
                              count(*(back-1),(y-1)*x*mid_y);
                              if (islower(*(back-1)))
                              {
                                   count(*(back-2),(y-1)*x*mid_y);
                                   back-=3;
                              }
                         }
                     }              
                }
           }
           i++;
      }
      int flag = 1;
      for(int k=0;k<52;k++)
      if(times[0][k]!=times[1][k]) flag = 0;
      if(flag) cout<<"YES" <<endl;
      else cout<<"NO" <<endl;
   }
   return 1;
}
void count(char c,int add)
{
     int index;
     if(c>65&&c<90) index = int(c)-65;
     else index = int (c)-97+26;
     times[side][index]+=add;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值