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

#include <bits/stdc++.h>
using namespace std;
int N, M, P;
typedef vector<int> vec;
int cnt = 0;
char S[200];
map<string, vec> mp;
map<string, string> mmp;
map<vec, string> vp;
void init() {
  cnt = 0;
  mp.clear();
  mmp.clear();
  vp.clear();
}

bool asset(char c) {
  if (S[cnt] == c) {
    cnt++;
    return true;
  }
  return false;
}

vec variable();
vec factor();
vec term();
vec formula();
vec operator*(const vec &a, const vec &b) {
  if (a.empty() || b.empty())
    return vec();
  vec x = vec(N);
  for (int i = 0; i < N; i++)
    x[i] = a[i] + b[i];
  return x;
}
vec operator/(const vec &a, const vec &b) {
  if (a.empty() || b.empty())
    return vec();
  vec x = vec(N);
  for (int i = 0; i < N; i++)
    x[i] = a[i] - b[i];
  return x;
}
bool check(const vec &a, const vec &b) {
  if (a.empty() || b.empty())
    return false;
  for (int i = 0; i < N; i++)
    if (a[i] != b[i])
      return false;
  return true;
}
vec variable() {
  string s = "";
  while (('a' <= S[cnt] && S[cnt] <= 'z') || ('A' <= S[cnt] && S[cnt] <= 'Z')) {
    s += S[cnt];
    asset(S[cnt]);
  }
  return mp[mmp[s]];
}
vec factor() {
  if (asset('(')) {
    vec v = formula();
    asset(')');
    return v;
  } else
    return variable();
}
vec term() {
  vec v = factor();
  while (true) {
    if (asset('*')) {
      vec nv = factor();
      v = v * nv;
    } else if (asset('/')) {
      vec nv = factor();
      v = v / nv;
    } else
      break;
  }
  return v;
}
vec formula() {
  vec v = term();
  while (true) {
    if (asset('+')) {
    } else if (asset('-')) {
    } else
      break;
    vec nv = term();
    if (!check(v, nv))
      return vec();
  }
  return v;
}

int main() {
  while (cin >> N >> M >> P && (N | M | P)) {
    init();
    for (int i = 0; i < M; i++) {
      string a;
      cin >> a;
      vec v = vec(N);
      for (int j = 0; j < N; j++) {
        int d;
        cin >> d;
        v[j] = d;
      }
      mp[a] = v;
      vp[v] = a;
    }
    cin >> S;
    for (int i = 0; i < P; i++) {
      string a, b;
      cin >> a >> b;
      mmp[a] = b;
    }
    vec v = formula();

    if (v.empty()) {
      cout << "error" << endl;
    } else if (vp.count(v)) {
      cout << vp[v] << endl;
    } else {
      cout << "undefined" << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N, M, P;
typedef vector<int> vec;
int cnt = 0;
char S[200];
map<string, vec> mp;
map<string, string> mmp;
map<vec, string> vp;
void init() {
  cnt = 0;
  mp.clear();
  mmp.clear();
  vp.clear();
}

bool asset(char c) {
  if (S[cnt] == c) {
    cnt++;
    return true;
  }
  return false;
}

vec variable();
vec factor();
vec term();
vec formula();
vec operator*(const vec &a, const vec &b) {
  vec x = vec(N);
  for (int i = 0; i < N; i++)
    x[i] = a[i] + b[i];
  return x;
}
vec operator/(const vec &a, const vec &b) {
  vec x = vec(N);
  for (int i = 0; i < N; i++)
    x[i] = a[i] - b[i];
  return x;
}
bool check(const vec &a, const vec &b) {
  for (int i = 0; i < N; i++)
    if (a[i] != b[i])
      return false;
  return true;
}
vec variable() {
  string s = "";
  while (('a' <= S[cnt] && S[cnt] <= 'z') || ('A' <= S[cnt] && S[cnt] <= 'Z')) {
    s += S[cnt];
    asset(S[cnt]);
  }
  return mp[mmp[s]];
}
vec factor() {
  if (asset('(')) {
    vec v = formula();
    asset(')');
    return v;
  } else
    return variable();
}
vec term() {
  vec v = factor();
  while (true) {
    if (asset('*')) {
      vec nv = factor();
      v = v * nv;
    } else if (asset('/')) {
      vec nv = factor();
      v = v / nv;
    } else
      break;
  }
  return v;
}
vec formula() {
  vec v = term();
  while (true) {
    if (asset('+')) {
    } else if (asset('-')) {
    } else
      break;
    vec nv = term();
    if (!check(v, nv))
      return vec();
  }
  return v;
}

int main() {
  while (cin >> N >> M >> P && (N | M | P)) {
    init();
    for (int i = 0; i < M; i++) {
      string a;
      cin >> a;
      vec v = vec(N);
      for (int j = 0; j < N; j++) {
        int d;
        cin >> d;
        v[j] = d;
      }
      mp[a] = v;
      vp[v] = a;
    }
    cin >> S;
    for (int i = 0; i < P; i++) {
      string a, b;
      cin >> a >> b;
      mmp[a] = b;
    }
    vec v = formula();

    if (v.empty()) {
      cout << "error" << endl;
    } else if (vp.count(v)) {
      cout << vp[v] << endl;
    } else {
      cout << "undefined" << endl;
    }
  }
}