您的位置:首页 > 大数据 > 人工智能

URAL 1027 D++ Again

2015-08-03 11:35 381 查看
大水题,,字符串匹配。。。。。。

可是我却一直WA。。。。。呜呜呜

优先看是不是注释,,,再看在不在括号里(判断括号里东西对不对),,,,其他的东西随便了。。。。

#include <iostream>

#include <fstream>

#include <sstream>

#include <vector>

#include <string>

#include <stack>

#include <algorithm>

#include <cstdio>

class Application

{

      string file;

      public:

      Application()

      {

            char rc;

            while ((rc=cin.get())!=EOF) file.push_back(rc);

      }

      int run()

      {

          bool comment=false;

          int counter=0;

          int last=~(1<<31);

          bool answer=true;

          for (int i=0;i<file.size();i++)

          {

              if (comment)

              {

                   if (file[i]=='*') last=i;

                   else if (file[i]==')'&&last+1==i) comment=false;

              }

              else if (counter)

              {

                 if (file[i]=='*'&&last+1==i)

                 {

                      counter--;

                      last=~(1<<31);

                      comment=true;

                 }

                 else if (file[i]==')')

                 {

                    counter--;

                 }

                 else if (file[i]=='(')

                 {

                      counter++;

                      last=i;

                 }

                 else

                 {

                     bool valid=false;

                     for (char* j="=+-*/0123456789)(\n";*j;j++)

                         if (file[i]==*j) valid=true;

                     if (!valid) answer=false;

                 }

              }

              else

              {

                  if (file[i]=='(')

                  {

                     counter++;

                     last=i;

                  }

                  else if (file[i]==')')

                  {

                       answer=false;

                  }

              }

          }

          if (comment||counter) answer=false;

          cout<<(char*)(answer?"YES":"NO")<<endl;

          return 0;

      }

};

int main()

{

    Application app;

    return app.run();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: