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

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

char str[100];

ll calc(ll lhs, char c, ll rhs) {
  if (c == '+') {
    return lhs + rhs;
  }
  return lhs * rhs;
}
char *skip(char *p) {
  while (isdigit(*p)) {
    p++;
  }
  return p;
}

ll calc1() {
  char *p = str;
  ll lhs = atoi(p);
  p = skip(p);
  while (*p != 0) {
    char c = *p;
    p++;
    ll rhs = atoi(p);
    lhs = calc(lhs, c, rhs);
    p = skip(p);
  }
  return lhs;
}

ll calc2() {
  char *p = str;
  vector<ll> vs;
  ll lhs = atoi(p);
  p = skip(p);
  while (*p != 0) {
    char c = *p;
    p++;
    if (c == '+') {
      vs.push_back(lhs);
      lhs = atoi(p);
    } else {
      ll rhs = atoi(p);
      lhs *= rhs;
    }
    p = skip(p);
  }
  FORIT(it, vs) { lhs += *it; }
  return lhs;
}

int main() {
  ll ans;
  while (scanf("%s %lld", str, &ans) > 0) {
    ll v1 = calc1();
    ll v2 = calc2();
    if (ans == v1 && ans == v2) {
      puts("U");
    } else if (ans == v1) {
      puts("L");
    } else if (ans == v2) {
      puts("M");
    } else {
      puts("I");
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

char str[100];

int calc(int lhs, char c, int rhs) {
  if (c == '+') {
    return lhs + rhs;
  }
  return lhs * rhs;
}
char *skip(char *p) {
  while (isdigit(*p)) {
    p++;
  }
  return p;
}

int calc1() {
  char *p = str;
  int lhs = atoi(p);
  p = skip(p);
  while (*p != 0) {
    char c = *p;
    p++;
    assert(p - str < 20);
    int rhs = atoi(p);
    lhs = calc(lhs, c, rhs);
    p = skip(p);
  }
  return lhs;
}

int calc2() {
  char *p = str;
  vector<int> vs;
  int lhs = atoi(p);
  p = skip(p);
  while (*p != 0) {
    char c = *p;
    p++;
    if (c == '+') {
      vs.push_back(lhs);
      lhs = atoi(p);
      assert(lhs != 0);
    } else {
      assert(lhs != 0);
      int rhs = atoi(p);
      lhs *= rhs;
    }
    p = skip(p);
  }
  FORIT(it, vs) { lhs += *it; }
  return lhs;
}

int main() {
  int ans;
  while (scanf("%s %d", str, &ans) > 0) {
    int v1 = calc1();
    int v2 = calc2();
    if (ans == v1 && ans == v2) {
      puts("U");
    } else if (ans == v1) {
      puts("L");
    } else if (ans == v2) {
      puts("M");
    } else {
      puts("I");
    }
  }
}