artemgf

Снова D++

Nov 12th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <stdio.h>
  9. #include <cmath>
  10. #include <math.h>
  11. #include <queue>
  12. #include <stack>
  13. #include <climits>
  14. #include <deque>
  15. #include <ctime>
  16.  
  17. using namespace std;
  18.  
  19. typedef long long ll;
  20. typedef unsigned long long ull;
  21. typedef unsigned int ui;
  22.  
  23. int main()
  24. {
  25.     string s;
  26.     ll f = 0, c = 0;
  27.     stack<int>last;
  28.     while (getline(cin, s))
  29.     {
  30.         for (int i = 0; i < s.size(); i++)
  31.         {
  32.             if (s[i] == '(')
  33.             {
  34.                 if (!last.empty())
  35.                 {
  36.                         if (s[i + 1] == '*'&&last.top() != 1)
  37.                         {
  38.                             c++;
  39.                             i++;
  40.                             last.push(1);
  41.                         }
  42.                         else
  43.                             if (!last.empty())
  44.                             {
  45.                                 if (last.top() != 1)
  46.                                 {
  47.                                     last.push(0);
  48.                                     f++;
  49.                                 }
  50.                             }
  51.                             else
  52.                             {
  53.                                 last.push(0);
  54.                                 f++;
  55.                             }
  56.                 }
  57.                 else
  58.                     if (last.empty())
  59.                     {
  60.                         if (s[i + 1] == '*')
  61.                         {
  62.                             c++;
  63.                             i++;
  64.                             last.push(1);
  65.                         }
  66.                         else
  67.                         {
  68.                             last.push(0);
  69.                             f++;
  70.                         }
  71.                     }
  72.             }
  73.             else
  74.             {
  75.                 if (s[i] == '*')
  76.                 {
  77.                     if (!last.empty())
  78.                         if (last.top() == 1)
  79.                             if (s[i + 1] == ')')
  80.                             {
  81.                                 c--;
  82.                                 last.pop();
  83.                                 i++;
  84.                             }
  85.                 }
  86.                 else
  87.                     if (s[i] == ')')
  88.                     {
  89.                         if (f == 0 && c == 0)
  90.                         {
  91.                             cout << "NO";
  92.                             return 0;
  93.                         }
  94.                         else
  95.                             if (last.top() == 0)
  96.                             {
  97.                                 f--;
  98.                                 last.pop();
  99.                             }
  100.                     }
  101.                             else
  102.                             {
  103.                                 if (!last.empty())
  104.                                 {
  105.                                     if (last.top() == 0)
  106.                                     {
  107.                                         if ((s[i] < '0' || s[i] > '9') && s[i] != '*' && s[i] != '-' && s[i] != '+' && s[i] != '/'&&s[i] != '=')
  108.                                         {
  109.                                             cout << "NO";
  110.                                             return 0;
  111.                                         }
  112.                                     }
  113.                                 }
  114.                             }
  115.             }
  116.         }
  117.        
  118.     }
  119.     if (c == 0 && f == 0)
  120.     {
  121.         cout << "YES";
  122.     }
  123.     else
  124.         cout << "NO";
  125.  
  126.  
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment