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

#include <cmath>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>

using namespace std;

string s;

string func(string s) {
  stack<char> st;
  string t = "";
  for (int i = 0; i < s.size(); i++) {
    if (isalpha(s[i]) || isdigit(s[i])) {
      t += s[i];
      while (st.size() && st.top() == '-')
        t += st.top(), st.pop();
    } else if (s[i] != ')')
      st.push(s[i]);
    else {
      while (st.top() != '(')
        t += st.top(), st.pop();
      st.pop();
      while (st.size() && st.top() == '-')
        t += st.top(), st.pop();
    }
  }
  return t;
}

string func2(string s, int p, int q, int r) {
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'P')
      s[i] = '0' + p;
    if (s[i] == 'Q')
      s[i] = '0' + q;
    if (s[i] == 'R')
      s[i] = '0' + r;
  }
  return s;
}

int solve(string s) {
  stack<int> st;
  for (int i = 0; i < s.size(); i++) {
    if (isdigit(s[i]))
      st.push(s[i] - '0');
    if (s[i] == '-') {
      int a = st.top();
      st.pop();
      a = 2 - a;
      st.push(a);
    }
    if (s[i] == '+') {
      int a = st.top();
      st.pop();
      int b = st.top();
      st.pop();
      a = max(a, b);
      st.push(a);
    }
    if (s[i] == '*') {
      int a = st.top();
      st.pop();
      int b = st.top();
      st.pop();
      a = min(a, b);
      st.push(a);
    }
  }
  if (st.top() == 2)
    return 1;
  else
    return 0;
}

int main(void) {
  while (cin >> s && s != ".") {
    s = func(s);
    int ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          string t = s;
          t = func2(t, i, j, k);
          ans += solve(t);
        }
      }
    }
    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>

using namespace std;

string s;

bool is_num(char c) {
  if ((c >= '0' && c <= '2') || (c >= 'P' && c <= 'R'))
    return true;
  else
    return false;
}

int func(string t) {
  for (int i = 0; i < t.size(); i++) {
    if (t[i] == '-') {
      t[i - 1] = '0' + ('2' - t[i - 1]);
      t = t.substr(0, i) + t.substr(i + 1);
      i -= 1;
    } else if (t[i] == '+') {
      t[i - 2] = '0' + max(t[i - 2] - '0', t[i - 1] - '0');
      t = t.substr(0, i - 1) + t.substr(i + 1);
      i -= 2;
    } else if (t[i] == '*') {
      t[i - 2] = '0' + min(t[i - 2] - '0', t[i - 1] - '0');
      t = t.substr(0, i - 1) + t.substr(i + 1);
      i -= 2;
    }
  }
  return t[0] - '0';
}

int main(void) {
  while (cin >> s && s != ".") {
    for (int i = 0; i < s.size() - 1; i++) {
      if (s[i] == '-' && s[i + 1] == '-')
        s = s.substr(0, i) + s.substr(i + 2);
    }
    stack<char> st;
    string t = "";
    for (int i = 0; i < s.size(); i++) {
      if (is_num(s[i]))
        t += s[i];
      if (s[i] == '+' || s[i] == '*' || s[i] == '-' || s[i] == '(')
        st.push(s[i]);
      if (s[i] == ')') {
        while (st.top() != '(')
          t += st.top(), st.pop();
        st.pop();
      }
      if (is_num(s[i]) && st.top() == '-')
        t += st.top(), st.pop();
    }
    s = t;

    int ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          t = s;
          for (int l = 0; l < t.size(); l++) {
            if (t[l] == 'P')
              t[l] = '0' + i;
            if (t[l] == 'Q')
              t[l] = '0' + j;
            if (t[l] == 'R')
              t[l] = '0' + k;
          }
          if (func(t) == 2)
            ans++;
        }
      }
    }
    cout << ans << endl;
  }
}