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

来源:互联网 发布:windows movie maker2 编辑:程序博客网 时间:2024/04/26 07:36
#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;}


原创粉丝点击