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

#include <bits/stdc++.h>
using namespace std;
map<string, vector<int>> sv;
map<vector<int>, string> vs;
map<string, string> ss;
vector<int> dfs();
int n, m, inp, d, p, f;
string s, t, t2;
bool ans;

vector<int> get() {
  vector<int> res(n);
  res[0] = (1e9);
  if (s[p] == '(')
    p++, res = dfs(), p++;
  else {
    string str;
    while (('a' <= s[p] && s[p] <= 'z') || ('A' <= s[p] && s[p] <= 'Z'))
      str += s[p++];
    res = sv[ss[str]];
  }
  return res;
}

vector<int> cal() {
  vector<int> res = get(), r;
  while (s[p] != ')') {
    if (s[p] == '*') {
      p++;
      r = get();
      for (int i = 0; i < n; i++)
        res[i] += r[i];
    } else if (s[p] == '/') {
      p++;
      r = get();
      for (int i = 0; i < n; i++)
        res[i] -= r[i];
    } else
      break;
  }
  return res;
}

vector<int> dfs() {
  vector<int> res = cal(), r(n);
  r[0] = (1e9);
  while (p < s.size() && s[p] != ')') {
    if (s[p] == '+' || s[p] == '-')
      p++;
    else
      r = cal();
    if (r[0] != (1e9) && res != r)
      ans = false;
  }
  return res;
}

int main() {
  while (1) {
    cin >> n >> m >> inp;
    if (!n && !m && !inp)
      break;
    for (int i = 0; i < m; i++) {
      cin >> t;
      for (int j = 0; j < n; j++) {
        cin >> d;
        sv[t].push_back(d);
      }
      vs[sv[t]] = t;
    }
    cin >> s;
    for (int i = 0; i < inp; i++) {
      cin >> t >> t2;
      ss[t] = t2;
    }
    s = '(' + s + ')';
    p = 0;
    ans = true;
    vector<int> r = dfs();
    if (!ans)
      cout << "error" << endl;
    else if (vs.count(r))
      cout << vs[r] << endl;
    else
      cout << "undefined" << endl;
    sv.clear();
    vs.clear();
    ss.clear();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
map<string, vector<int>> sv;
map<vector<int>, string> vs;
map<string, string> ss;
vector<int> dfs();
int n, m, inp, d, p, f;
string s, t, t2;
bool ans;

vector<int> get() {
  vector<int> res(n);
  res[0] = (1e9);
  if (s[p] == '(')
    p++, res = dfs(), p++;
  else {
    string str;
    while (('a' <= s[p] && s[p] <= 'z') || ('A' <= s[p] && s[p] <= 'Z'))
      str += s[p++];
    res = sv[ss[str]];
  }
  return res;
}

vector<int> cal() {
  vector<int> res = get(), r;
  while (s[p] != ')') {
    if (s[p] == '*') {
      p++;
      r = get();
      for (int i = 0; i < n; i++)
        res[i] += r[i];
    } else if (s[p] == '/') {
      p++;
      r = get();
      for (int i = 0; i < n; i++)
        res[i] -= r[i];
    } else
      break;
  }
  return res;
}

vector<int> dfs() {
  vector<int> res = cal(), r(n);
  r[0] = (1e9);
  while (p < s.size() && s[p] != ')') {
    if (s[p] == '(') {
      p++;
      r = dfs();
      p++;
    } else if (s[p] == '+' || s[p] == '-')
      p++;
    else
      r = cal();
    if (res[0] != (1e9) && r[0] != (1e9) && res != r)
      ans = false;
    if (r[0] != (1e9))
      res = r;
  }
  return res;
}

int main() {
  while (1) {
    cin >> n >> m >> inp;
    if (!n && !m && !inp)
      break;
    for (int i = 0; i < m; i++) {
      cin >> t;
      for (int j = 0; j < n; j++) {
        cin >> d;
        sv[t].push_back(d);
      }
      vs[sv[t]] = t;
    }
    cin >> s;
    for (int i = 0; i < inp; i++) {
      cin >> t >> t2;
      ss[t] = t2;
    }
    s = '(' + s + ')';
    p = 0;
    ans = true;
    vector<int> r = dfs();
    if (!ans)
      cout << "error" << endl;
    else if (vs.count(r))
      cout << vs[r] << endl;
    else
      cout << "undefined" << endl;
    sv.clear();
    vs.clear();
    ss.clear();
  }
  return 0;
}