Problem p00672 (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))

int n, m, p;
int pos;
char str[10000];
map<string, vector<int>> mapto;
map<vector<int>, string> mapfrom;
map<string, string> convert;
vector<int> error;

vector<int> formula();
vector<int> factor() {
  vector<int> lhs;
  if (str[pos] == '(') {
    pos++;
    lhs = formula();
    pos++;
  } else {
    string s;
    while (isalpha(str[pos])) {
      s += str[pos++];
    }
    if (!convert.count(s)) {
      return error;
    }
    if (!mapto.count(convert[s])) {
      return error;
    }
    lhs = mapto[convert[s]];
  }
  return lhs;
}

vector<int> term() {
  vector<int> lhs = factor();
  if (lhs == error) {
    return error;
  }
  while (str[pos] == '*' || str[pos] == '/') {
    int s = str[pos] == '*' ? 1 : -1;
    pos++;
    vector<int> rhs = factor();
    if (rhs == error) {
      return error;
    }
    REP(i, n) { lhs[i] += s * rhs[i]; }
  }
  return lhs;
}

vector<int> formula() {
  vector<int> lhs = term();
  if (lhs == error) {
    return error;
  }
  while (str[pos] == '+' || str[pos] == '-') {
    pos++;
    vector<int> rhs = term();
    if (rhs == error) {
      return error;
    }
    REP(i, n) {
      if (lhs[i] != rhs[i]) {
        return error;
      }
    }
  }
  return lhs;
}

int main() {
  error = vector<int>(1, -1000000000);
  while (scanf("%d %d %d", &n, &m, &p), n | m | p) {
    pos = 0;
    mapto.clear();
    mapfrom.clear();
    REP(i, m) {
      string s;
      cin >> s;
      vector<int> vect(n, 0);
      REP(i, n) { scanf("%d", &vect[i]); }
      mapto[s] = vect;
      mapfrom[vect] = s;
    }
    scanf("%s", str);
    REP(i, p) {
      string from, to;
      cin >> from >> to;
      convert[from] = to;
    }
    vector<int> ans = formula();
    while (pos > strlen(str))
      ;
    if (ans[0] <= -100000) {
      puts("error");
    } else if (mapfrom.count(ans)) {
      printf("%s\\n", mapfrom[ans].c_str());
    } else {
      puts("undefined");
    }
  }
}

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))

int n, m, p;
int pos;
char str[10000];
map<string, vector<int>> mapto;
map<vector<int>, string> mapfrom;
map<string, string> convert;
vector<int> error;

vector<int> formula();
vector<int> factor() {
  vector<int> lhs;
  if (str[pos] == '(') {
    pos++;
    lhs = formula();
    assert(str[pos] == ')');
    pos++;
  } else {
    string s;
    while (isalpha(str[pos])) {
      s += str[pos++];
    }
    if (!convert.count(s)) {
      return error;
    }
    if (!mapto.count(convert[s])) {
      return error;
    }
    lhs = mapto[convert[s]];
  }
  return lhs;
}

vector<int> term() {
  vector<int> lhs = factor();
  if (lhs == error) {
    return error;
  }
  while (str[pos] == '*' || str[pos] == '/') {
    int s = str[pos] == '*' ? 1 : -1;
    pos++;
    vector<int> rhs = factor();
    if (rhs == error) {
      return error;
    }
    REP(i, n) { lhs[i] += s * rhs[i]; }
  }
  return lhs;
}

vector<int> formula() {
  vector<int> lhs = term();
  if (lhs == error) {
    return error;
  }
  while (str[pos] == '+' || str[pos] == '-') {
    pos++;
    vector<int> rhs = term();
    if (rhs == error) {
      return error;
    }
    REP(i, n) {
      if (lhs[i] != rhs[i]) {
        return error;
      }
    }
  }
  return lhs;
}

int main() {
  error = vector<int>(1, -1000000000);
  while (scanf("%d %d %d", &n, &m, &p), n | m | p) {
    pos = 0;
    mapto.clear();
    mapfrom.clear();
    REP(i, m) {
      string s;
      cin >> s;
      vector<int> vect(n, 0);
      REP(i, n) { scanf("%d", &vect[i]); }
      mapto[s] = vect;
      mapfrom[vect] = s;
    }
    scanf("%s", str);
    REP(i, p) {
      string from, to;
      cin >> from >> to;
      convert[from] = to;
    }
    vector<int> ans = formula();
    if (ans[0] <= -100000) {
      puts("error");
    } else if (mapfrom.count(ans)) {
      printf("%s\\n", mapfrom[ans].c_str());
    } else {
      puts("undefined");
    }
  }
}