Problem p01725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef vector<long long int> VI;
typedef vector<vector<long long int>> VVI;
typedef vector<string> VS;
typedef pair<long long int, long long int> PII;
typedef pair<char, PII> PCI;
typedef pair<long long int, PII> PIII;

const long long int pri[13][3] = {
    {1, 1, 1}, {1, 1, 2}, {1, 2, 1}, {2, 1, 1}, {1, 2, 3}, {1, 3, 2}, {2, 1, 3},
    {2, 3, 1}, {3, 1, 2}, {3, 2, 1}, {2, 2, 1}, {2, 1, 2}, {1, 2, 2}};

// string??????long long int????????????
long long int string_to_lli(string str) {
  long long int length = str.length();
  long long int number = 0;
  for (long long int i = 0; i < length; i++) {
    number += (long long int)(str[i] - '0');
    number *= 10;
  }
  number /= 10;
  return number;
}

long long int check(char c) {
  if (c >= '0' && c <= '9') {
    return 0;
  } else if (c == '+' || c == '-' || c == '*') {
    return 1;
  } else {
    return 2;
  }
}

long long int rep_pri(char c, long long int i) {
  if (c == '+') {
    return pri[i][0];
  } else if (c == '-') {
    return pri[i][1];
  } else if (c == '*') {
    return pri[i][2];
  } else {
    std::cerr << "ERROR_REP_PRI" << endl;
    return -1;
  }
}

long long int calc(long long int first, long long int second, char ope) {
  if (ope == '+') {
    return first + second;
  } else if (ope == '-') {
    return first - second;
  } else if (ope == '*') {
    return first * second;
  } else {
    std::cerr << "ERROR_CALC" << endl;
    return -1;
  }
}

long long int act(string str, long long int i) {
  long long int ans;
  long long int len = (long long int)str.length();
  stack<long long int> num;
  stack<char> ope;
  for (long long int j = 0; j < len; j++) {
    // cout << "J = " << j << endl;
    // cout << "STR[j] = " << str[j] << endl;
    if (check(str[j]) == 0) {
      long long int start = j;
      long long int end = j;
      while (check(str[end]) == 0) {
        end++;
      }
      string sub = str.substr(start, end - start);
      num.push(string_to_lli(sub));
      j = end - 1;
      continue;
    } else if (check(str[j]) == 1) {
      if (ope.empty()) {
        ope.push(str[j]);
        continue;
      } else {
        char now = str[j];
        while ((!ope.empty()) && (rep_pri(ope.top(), i) >= rep_pri(now, i))) {
          long long int second = num.top();
          num.pop();
          long long int first = num.top();
          num.pop();
          char old = ope.top();
          ope.pop();

          // cout << old << " " << rep_pri(old, i) << endl;
          // cout << now << " " << rep_pri(now, i) << endl;

          num.push(calc(first, second, old));
          // cout << first << " " << old << " " << second << " " << calc(first,
          // second, old) << endl; cout << now << endl;
        }
        ope.push(now);
        continue;
      }
    } else if (str[j] == '(') {
      long long int start = j;
      int in = 1;
      int out = 0;
      // cout <<"START = "<<  str[j] << endl;
      while (in != out) {
        // cout << str[j] << endl;
        j++;
        if (str[j] == '(') {
          in++;
        } else if (str[j] == ')') {
          out++;
        }
      }
      long long int end = j;
      // cout << start << " " << end << endl;
      string sub = str.substr(start + 1, end - start - 1);
      // cout << sub << endl;
      // cout << "REC_IN" << endl;
      num.push(act(sub, i));
      // cout << "REC_OUT" << endl;
    } else {
      // cout << "ERROR = " << str[j] << endl;
      std::cerr << "ERROR_ACT" << endl;
    }
  }

  while (!ope.empty()) {
    long long int second = num.top();
    num.pop();
    long long int first = num.top();
    num.pop();
    char ope_now = ope.top();
    // cout << first << " " << ope_now << " " << second << endl;
    ope.pop();
    num.push(calc(first, second, ope_now));
  }
  ans = num.top();
  return ans;
}

int main(void) {
  string str;
  cin >> str;

  long long int max_res;
  for (long long int i = 0; i < 13; i++) {
    // ans??¨max_res???????????????
    long long int ans = act(str, i);
    if (i == 0) {
      max_res = ans;
    } else {
      if (ans > max_res) {
        max_res = ans;
      }
    }
  }
  cout << max_res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef vector<long long int> VI;
typedef vector<vector<long long int>> VVI;
typedef vector<string> VS;
typedef pair<long long int, long long int> PII;
typedef pair<char, PII> PCI;
typedef pair<long long int, PII> PIII;

const long long int pri[13][3] = {
    {1, 1, 1}, {1, 1, 2}, {1, 2, 1}, {2, 1, 1}, {1, 2, 3}, {1, 3, 2}, {2, 1, 3},
    {2, 3, 1}, {3, 1, 2}, {3, 2, 1}, {2, 2, 1}, {2, 1, 2}, {1, 2, 2}};

// string??????long long int????????????
long long int string_to_lli(string str) {
  long long int length = str.length();
  long long int number = 0;
  for (long long int i = 0; i < length; i++) {
    number += (long long int)(str[i] - '0');
    number *= 10;
  }
  number /= 10;
  return number;
}

long long int check(char c) {
  if (c >= '0' && c <= '9') {
    return 0;
  } else if (c == '+' || c == '-' || c == '*') {
    return 1;
  } else {
    return 2;
  }
}

long long int rep_pri(char c, long long int i) {
  if (c == '+') {
    return pri[i][0];
  } else if (c == '-') {
    return pri[i][1];
  } else if (c == '*') {
    return pri[i][2];
  } else {
    std::cerr << "ERROR_REP_PRI" << endl;
    return -1;
  }
}

long long int calc(long long int first, long long int second, char ope) {
  if (ope == '+') {
    return first + second;
  } else if (ope == '-') {
    return first - second;
  } else if (ope == '*') {
    return first * second;
  } else {
    std::cerr << "ERROR_CALC" << endl;
    return -1;
  }
}

long long int act(string str, long long int i) {
  long long int ans;
  long long int len = (long long int)str.length();
  stack<long long int> num;
  stack<char> ope;
  for (long long int j = 0; j < len; j++) {
    // cout << "J = " << j << endl;
    // cout << "STR[j] = " << str[j] << endl;
    if (check(str[j]) == 0) {
      long long int start = j;
      long long int end = j;
      while (check(str[end]) == 0) {
        end++;
      }
      string sub = str.substr(start, end - start);
      num.push(string_to_lli(sub));
      j = end - 1;
      continue;
    } else if (check(str[j]) == 1) {
      if (ope.empty()) {
        ope.push(str[j]);
        continue;
      } else {
        char now = str[j];
        while ((!ope.empty()) && (rep_pri(ope.top(), i) >= rep_pri(now, i))) {
          long long int second = num.top();
          num.pop();
          long long int first = num.top();
          num.pop();
          char old = ope.top();
          ope.pop();

          // cout << old << " " << rep_pri(old, i) << endl;
          // cout << now << " " << rep_pri(now, i) << endl;

          num.push(calc(first, second, old));
          // cout << first << " " << old << " " << second << " " << calc(first,
          // second, old) << endl; cout << now << endl;
        }
        ope.push(now);
        continue;
      }
    } else if (str[j] == '(') {
      long long int start = j;
      while (str[j] != ')') {
        j++;
      }
      long long int end = j;
      string sub = str.substr(start + 1, end - start - 1);
      // cout << sub << endl;
      // cout << "REC_IN" << endl;
      num.push(act(sub, i));
      // cout << "REC_OUT" << endl;
    } else {
      std::cerr << "ERROR_ACT" << endl;
    }
  }

  while (!ope.empty()) {
    long long int second = num.top();
    num.pop();
    long long int first = num.top();
    num.pop();
    char ope_now = ope.top();
    // cout << first << " " << ope_now << " " << second << endl;
    ope.pop();
    num.push(calc(first, second, ope_now));
  }
  ans = num.top();
  return ans;
}

int main(void) {
  string str;
  cin >> str;
  long long int max_res = -1e09;

  for (long long int i = 0; i < 13; i++) {
    // ans??¨max_res???????????????
    long long int ans = act(str, i);
    // cout << "I = " << i << endl;
    // cout << "ANS = " << ans << endl;
    if (ans > max_res) {
      max_res = ans;
    }
  }
  cout << max_res << endl;
  return 0;
}