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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 1000

int calc(int a, int op, int b) {
  if (op == -1)
    return a + b;
  return a * b;
}

bool isop(int c) { return c <= -1; }

bool solve() {
  int n;

  scanf("%d", &n);

  if (n == 0)
    return false;

  stack<pair<int, int>> st;

  for (int i = 0; i <= n; i++) {
    char s[200];
    int counter = 0;

    if (i < n) {
      scanf("%s", s);
      while (s[counter] == '.')
        counter++;
    } else {
      counter = 0;
    }

    // debug(s[counter]);

    vector<int> vec;
    while (st.size() &&
           (st.top().first > counter ||
            (st.top().first == counter && isop(st.top().second)))) {
      auto p = st.top();
      st.pop();

      if (isop(p.second)) {
        int res = vec[0];
        for (int j = 1; j < vec.size(); j++)
          res = calc(res, p.second, vec[j]);
        st.push({p.first, res});
        vec = vector<int>();
      } else {
        vec.push_back(p.second);
      }
    }

    if (s[counter] == '+')
      st.push({counter, -1});
    else if (s[counter] == '*')
      st.push({counter, -2});
    else
      st.push({counter, s[counter] - '0'});
  }

  st.pop();

  printf("%d\\n", st.top().second);

  return true;
}

int main() {
  while (solve())
    ;

  return 0;
}


Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 1000

int calc(int a, int op, int b) {
  if (op == -1)
    return a + b;
  return a * b;
}

bool isop(char c) { return c <= -1; }

bool solve() {
  int n;

  scanf("%d", &n);

  if (n == 0)
    return false;

  stack<pair<int, int>> st;

  st.push({-1, '+'});

  for (int i = 0; i <= n; i++) {
    char s[200];
    int counter = 0;

    if (i < n) {
      scanf("%s", s);
      while (s[counter] == '.')
        counter++;
    } else {
      counter = 0;
    }

    // debug(s[counter]);

    vector<int> vec;
    while (st.size() &&
           (st.top().first > counter ||
            (st.top().first == counter && isop(st.top().second)))) {
      auto p = st.top();
      st.pop();

      if (isop(p.second)) {
        int res = vec[0];
        for (int j = 1; j < vec.size(); j++)
          res = calc(res, p.second, vec[j]);
        st.push({p.first, res});
        vec = vector<int>();
      } else {
        vec.push_back(p.second);
      }
    }

    if (s[counter] == '+')
      st.push({counter, -1});
    else if (s[counter] == '*')
      st.push({counter, -2});
    else
      st.push({counter, s[counter] - '0'});
  }

  st.pop();

  printf("%d\\n", st.top().second);

  return true;
}

int main() {
  while (solve())
    ;

  return 0;
}