Problem p01725 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef string::const_iterator State;

map<char, int> op;

ll term(State &, int);
ll number(State &);

ll calc(ll x, char c, ll y) {
  if (c == '+')
    x += y;
  if (c == '-')
    x -= y;
  if (c == '*')
    x *= y;
  return x;
}

ll term(State &begin, int d) {
  ll ret = term(begin, d + 1);
  while (1) {
    if (d == 4) {
      if (*begin == '(') {
        begin++;
        ll ret = term(begin, 1);
        begin++;
        return ret;
      } else {
        return number(begin);
      }
    } else if (op[*begin] == d) {
      char c = *begin;
      begin++;
      ret = calc(ret, c, term(begin, d + 1));
    } else {
      break;
    }
  }
  return ret;
}

ll number(State &begin) {
  int ret = 0;
  while (isdigit(*begin)) {
    ret *= 10;
    ret += *begin - '0';
    begin++;
  }
  return ret;
}

int main() {
  ll res = 0;
  bool f = 1;
  string s;
  cin >> s;
  REP(i, 3) REP(j, 3) REP(k, 3) {
    op['+'] = i + 1;
    op['-'] = j + 1;
    op['*'] = k + 1;
    State begin = s.begin();
    if (f)
      res = term(begin, 1), f = 0;
    else
      res = max(res, term(begin, 1));
  }
  cout << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef string::const_iterator State;

map<char, int> op;

ll term(State &, int);
ll number(State &);

ll calc(ll x, char c, ll y) {
  if (c == '+')
    x += y;
  if (c == '-')
    x -= y;
  if (c == '*')
    x *= y;
  return x;
}

ll term(State &begin, int d) {
  ll ret;
  if (d < 4)
    ret = term(begin, d + 1);
  while (1) {
    if (d == 4) {
      if (*begin == '(') {
        begin++;
        ll ret = term(begin, 1);
        begin++;
        return ret;
      } else {
        return number(begin);
      }
    } else if (op[*begin] == d) {
      char c = *begin;
      begin++;
      ret = calc(ret, c, term(begin, d + 1));
    } else {
      break;
    }
  }
  return ret;
}

ll number(State &begin) {
  int ret = 0;
  while (isdigit(*begin)) {
    ret *= 10;
    ret += *begin - '0';
    begin++;
  }
  return ret;
}

int main() {
  ll res = 0;
  bool f = 1;
  string s;
  cin >> s;
  REP(i, 3) REP(j, 3) REP(k, 3) {
    op['+'] = i + 1;
    op['-'] = j + 1;
    op['*'] = k + 1;
    State begin = s.begin();
    if (f)
      res = term(begin, 1), f = 0;
    else
      res = max(res, term(begin, 1));
  }
  cout << res << endl;

  return 0;
}