fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. #define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
  6.  
  7. const int maxn = 1e3 + 5;
  8.  
  9. int n, m;
  10. int val[maxn][maxn];
  11.  
  12.  
  13.  
  14.  
  15.  
  16. // bool isnumber(string s) {
  17. // for (char &x : s) {
  18. // if (!isdigit(x)) return 0;
  19. // }
  20. // return 1;
  21. // }
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. void solve() {
  30. string s;
  31. cin >> s;
  32.  
  33. // s += ' ';
  34. int n = s.size();
  35.  
  36. s = ' ' + s;
  37.  
  38. string xau = "";
  39. stack<string> st;
  40. FOR(i, 1, n) {
  41. // cerr << s[i] << ' ';
  42.  
  43. if (s[i] == '(') {
  44. st.push(xau); xau = "";
  45. continue;
  46. }
  47.  
  48. if ((s[i] == ',' || (s[i] == ')')) && isdigit(s[i - 1])) {
  49. st.push(xau); xau = "";
  50.  
  51.  
  52. int u = 0, v = 0;
  53. if (st.size()) u = stoll(st.top()); st.pop();
  54.  
  55. v = st.top()[0] - 'A' + 1; st.pop();
  56.  
  57.  
  58. st.push(to_string(val[u][v]));
  59.  
  60. if (s[i] == ')') {
  61. long long x = stoll(st.top()); st.pop();
  62. long long y = stoll(st.top()); st.pop();
  63.  
  64. string type = st.top(); st.pop();
  65.  
  66. if (type == "MAX") st.push(to_string(max(x, y)));
  67. else st.push(to_string(x + y));
  68.  
  69. }
  70.  
  71. continue;
  72. }
  73.  
  74.  
  75. if (s[i] == ',') continue;
  76.  
  77. if (i > 0 && isdigit(s[i]) && isalpha(s[i - 1])) {
  78. st.push(xau), xau = "";
  79. xau += s[i];
  80. continue;
  81. }
  82.  
  83.  
  84. xau += s[i];
  85.  
  86. }
  87.  
  88.  
  89. if (st.size() == 1) return cout << st.top() << '\n', void();
  90.  
  91.  
  92.  
  93. long long x = stoll(st.top()); st.pop();
  94. long long y = stoll(st.top()); st.pop();
  95. string type = st.top(); st.pop();
  96.  
  97.  
  98. long long ans = 0;
  99. if (type == "MAX") ans = max(x, y);
  100. else ans = x + y;
  101.  
  102. cout << ans << '\n';
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. int test;
  112. signed main() {
  113. ios::sync_with_stdio(0);
  114. cin.tie(0); cout.tie(0);
  115.  
  116.  
  117. cin >> n >> m;
  118.  
  119.  
  120. FOR(i, 1, n) FOR(j, 1, m) cin >> val[i][j];
  121.  
  122.  
  123. cin >> test;
  124. while (test--) solve();
  125.  
  126.  
  127. return (0 & 20-04-2009);
  128. }
  129.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty