fork download
  1. /*
  2. * @Author: hungeazy
  3. * @Date: 2023-11-22 19:26:24
  4. * @Last Modified by: hungeazy
  5. * @Last Modified time: 2024-10-03 10:21:16
  6. */
  7. #include <bits/stdc++.h>
  8. #pragma GCC optimize("O3")
  9. #pragma GCC optimize("unroll-loops")
  10. #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
  11. using namespace std;
  12. #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  13. #define int long long
  14. #define ull unsigned long long
  15. #define sz(x) x.size()
  16. #define sqr(x) (1LL * (x) * (x))
  17. #define all(x) x.begin(), x.end()
  18. #define fill(f,x) memset(f,x,sizeof(f))
  19. #define FOR(i,l,r) for(int i=l;i<=r;i++)
  20. #define FOD(i,r,l) for(int i=r;i>=l;i--)
  21. #define ii pair<int,int>
  22. #define iii pair<int,ii>
  23. #define di pair<ii,ii>
  24. #define vi vector<int>
  25. #define vii vector<ii>
  26. #define mii map<int,int>
  27. #define fi first
  28. #define se second
  29. #define pb push_back
  30. #define MOD 1000000007
  31. #define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
  32. #define YES cout << "YES\n"
  33. #define NO cout << "NO\n"
  34. #define MASK(i) (1LL << (i))
  35. #define c_bit(i) __builtin_popcountll(i)
  36. #define BIT(x,i) ((x) & MASK(i))
  37. #define SET_ON(x,i) ((x) | MASK(i))
  38. #define SET_OFF(x,i) ((x) & ~MASK(i))
  39. #define oo 1e18
  40. #define name "BIEUTHUC"
  41. #define endl '\n'
  42. #define time() cout << "-------------Time:" << 1000.0 * clock() / CLOCKS_PER_SEC << "ms.";
  43. template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
  44. template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
  45.  
  46. int tinh(int x, int y, char c)
  47. {
  48. if (c == '+') return x+y;
  49. if (c == '-') return x-y;
  50. if (c == 'x') return x*y;
  51. if (c == ':' and y != 0) return x/y;
  52. else return 0;
  53. }
  54.  
  55. int level(char c)
  56. {
  57. if (c == '+' or c == '-') return 1;
  58. if (c == 'x' or c == ':') return 2;
  59. return 0;
  60. }
  61.  
  62. int calc(string s)
  63. {
  64. int i = 0;
  65. stack<int> a; stack<char> b;
  66. FOR(i,0,s.size()-1)
  67. {
  68. if (s[i] == '(') b.push(s[i]);
  69. else if (isdigit(s[i]))
  70. {
  71. int x = 0;
  72. while (i < s.size() and isdigit(s[i])) x = x*10+(s[i]-'0'), i++;
  73. a.push(x); i--;
  74. }
  75. else if (s[i] == ')')
  76. {
  77. while (!b.empty() and b.top() != '(')
  78. {
  79. int x = a.top(); a.pop();
  80. int y = a.top(); a.pop();
  81. char z = b.top(); b.pop();
  82. a.push(tinh(y,x,z));
  83. }
  84. if (!b.empty()) b.pop();
  85. }
  86. else
  87. {
  88. while (!b.empty() and level(b.top()) >= level(s[i]))
  89. {
  90. int x = a.top(); a.pop();
  91. int y = a.top(); a.pop();
  92. char z = b.top(); b.pop();
  93. a.push(tinh(y,x,z));
  94. }
  95. b.push(s[i]);
  96. }
  97. }
  98. while (!b.empty())
  99. {
  100. int x = a.top(); a.pop();
  101. int y = a.top(); a.pop();
  102. char z = b.top(); b.pop();
  103. // cout << x << " " << y << ' ' << z << endl;
  104. a.push(tinh(y,x,z));
  105. }
  106. return (a.empty()?0:a.top());
  107. }
  108.  
  109. signed main()
  110. {
  111. fast;
  112. if (fopen(name".inp","r"))
  113. {
  114. freopen(name".inp","r",stdin);
  115. freopen(name".out","w",stdout);
  116. }
  117. string s;
  118. cin >> s;
  119. if (s.size() == 0) cout << 0;
  120. else cout << calc(s);
  121. return 0;
  122. }
  123. // ██░ ██ █ ██ ███▄ █ ▄████
  124. //▓██░ ██▒ ██ ▓██▒ ██ ▀█ █ ██▒ ▀█▒
  125. //▒██▀▀██░▓██ ▒██░▓██ ▀█ ██▒▒██░▄▄▄░
  126. //░▓█ ░██ ▓▓█ ░██░▓██▒ ▐▌██▒░▓█ ██▓
  127. //░▓█▒░██▓▒▒█████▓ ▒██░ ▓██░░▒▓███▀▒
  128. // ▒ ░░▒░▒░▒▓▒ ▒ ▒ ░ ▒░ ▒ ▒ ░▒ ▒
  129. // ▒ ░▒░ ░░░▒░ ░ ░ ░ ░░ ░ ▒░ ░ ░
  130. // ░ ░░ ░ ░░░ ░ ░ ░ ░ ░ ░ ░ ░
  131. // ░ ░ ░ ░ ░ ░
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty