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

#include <bits/stdc++.h>
using namespace std;
typedef pair<char, int> P;
typedef vector<P> vec;
typedef pair<int, vec> PP;
typedef vector<PP> mat;
string s;
int p, len;

mat add(mat a, mat b) {
  map<vec, int> mp;

  for (int i = 0; i < (int)a.size(); i++) {
    mp[a[i].second] += a[i].first;
  }

  for (int i = 0; i < (int)b.size(); i++) {
    mp[b[i].second] += b[i].first;
  }

  mat res;
  map<vec, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    if (it->second == 0)
      continue;
    res.push_back(PP(it->second, it->first));
  }

  sort(res.begin(), res.end());

  return res;
}

PP mul(PP a, PP b) {
  int fi = a.first * b.first;
  vec &va = a.second;
  vec &vb = b.second;
  map<char, int> mp;
  for (int i = 0; i < (int)va.size(); i++) {
    P p = va[i];
    mp[p.first] += p.second;
  }
  for (int j = 0; j < (int)vb.size(); j++) {
    P p = vb[j];
    mp[p.first] += p.second;
  }

  vec se;

  if (fi == 0) {
    return PP(fi, se);
  }

  map<char, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    se.push_back(*it);
  }
  return PP(fi, se);
}

mat mul(mat a, mat b) {
  map<vec, int> mp;
  for (int i = 0; i < (int)a.size(); i++) {
    PP as = a[i];
    for (int j = 0; j < (int)b.size(); j++) {
      PP bs = b[j];

      PP c = mul(as, bs);
      mp[c.second] += c.first;
    }
  }

  mat res;
  map<vec, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    if (it->second == 0)
      continue;
    res.push_back(PP(it->second, it->first));
  }
  return res;
}

mat target;
mat solve();

mat getNum(bool flg = false) {
  while (s[p] == ' ')
    p++;

  if ('0' <= s[p] && s[p] <= '9') {
    int res = 0;
    while ('0' <= s[p] && s[p] <= '9') {
      res = res * 10 + (s[p] - '0');
      p++;
    }
    mat re;
    vec r;
    if (res > 0)
      re.push_back(PP(res, r));
    return re;
  }

  if ('(' == s[p]) {
    p++;
    mat res = solve();
    p++;
    return res;
  }

  char ch = s[p++];
  while (s[p] == ' ')
    p++;
  int num = 1;
  if (s[p] == '^') {
    p++;
    mat tmp = getNum(true);
    num = tmp[0].first;
  }
  mat res;
  vec r;
  r.push_back(P(ch, num));
  res.push_back(PP(1, r));
  return res;
}

mat solve() {
  while (s[p] == ' ')
    p++;

  stack<mat> st;
  st.push(getNum());

  while (1) {
    while (s[p] == ' ')
      p++;

    if (p >= len) {
      break;
    } else if (s[p] == '+') {
      p++;
      mat num = getNum();
      st.push(num);
    } else if (s[p] == '-') {
      p++;
      mat num = getNum();
      for (int i = 0; i < (int)num.size(); i++)
        num[i].first *= -1;
      st.push(num);

    } else if (s[p] == ')') {
      break;
    } else {
      mat num = getNum();
      mat A = st.top();
      st.pop();
      st.push(mul(A, num));
    }
  }
  mat res = st.top();
  st.pop();
  while (!st.empty()) {
    res = add(res, st.top());
    st.pop();
  }

  for (int i = 0; i < (int)res.size(); i++) {
    sort(res[i].second.begin(), res[i].second.end());
  }
  sort(res.begin(), res.end());
  return res;
}

int main() {
  while (1) {
    getline(cin, s);
    if (s == ".")
      break;
    p = 0;
    len = s.size();
    target = solve();

    while (1) {
      getline(cin, s);
      if (s == ".")
        break;
      p = 0;
      len = s.size();
      mat tmp = solve();
      if (target == tmp)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
    cout << '.' << endl;
  }
  return 0;
}

Error: -6
2ccb9be2-3ee4-4c17-88a6-1889c5d36463.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00812/C++/s225531824.cpp:101: mat getNum(bool): Assertion `flg==false' failed.


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<char, int> P;
typedef vector<P> vec;
typedef pair<int, vec> PP;
typedef vector<PP> mat;
string s;
int p, len;

mat add(mat a, mat b) {
  map<vec, int> mp;

  for (int i = 0; i < (int)a.size(); i++) {
    mp[a[i].second] += a[i].first;
  }

  for (int i = 0; i < (int)b.size(); i++) {
    mp[b[i].second] += b[i].first;
  }

  mat res;
  map<vec, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    if (it->second == 0)
      continue;
    res.push_back(PP(it->second, it->first));
  }

  sort(res.begin(), res.end());

  return res;
}

PP mul(PP a, PP b) {
  int fi = a.first * b.first;
  vec &va = a.second;
  vec &vb = b.second;
  map<char, int> mp;
  for (int i = 0; i < (int)va.size(); i++) {
    P p = va[i];
    mp[p.first] += p.second;
  }
  for (int j = 0; j < (int)vb.size(); j++) {
    P p = vb[j];
    mp[p.first] += p.second;
  }

  vec se;

  if (fi == 0) {
    return PP(fi, se);
  }

  map<char, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    se.push_back(*it);
  }
  return PP(fi, se);
}

mat mul(mat a, mat b) {
  map<vec, int> mp;
  for (int i = 0; i < (int)a.size(); i++) {
    PP as = a[i];
    for (int j = 0; j < (int)b.size(); j++) {
      PP bs = b[j];

      PP c = mul(as, bs);
      mp[c.second] += c.first;
    }
  }

  mat res;
  map<vec, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    if (it->second == 0)
      continue;
    res.push_back(PP(it->second, it->first));
  }
  return res;
}

mat target;
mat solve();

mat getNum(bool flg = false) {
  while (s[p] == ' ')
    p++;

  if ('0' <= s[p] && s[p] <= '9') {
    int res = 0;
    while ('0' <= s[p] && s[p] <= '9') {
      res = res * 10 + (s[p] - '0');
      p++;
    }
    mat re;
    vec r;
    if (res > 0)
      re.push_back(PP(res, r));
    return re;
  }

  assert(flg == false);

  if ('(' == s[p]) {
    p++;
    mat res = solve();
    p++;
    return res;
  }

  char ch = s[p++];
  while (s[p] == ' ')
    p++;
  int num = 1;
  if (s[p] == '^') {
    mat tmp = getNum(true);
    num = tmp[0].first;
    assert(num > 0);
  }
  mat res;
  vec r;
  r.push_back(P(ch, num));
  res.push_back(PP(1, r));
  return res;
}

mat solve() {
  while (s[p] == ' ')
    p++;

  stack<mat> st;
  st.push(getNum());

  while (1) {
    while (s[p] == ' ')
      p++;

    if (p >= len) {
      break;
    } else if (s[p] == '+') {
      p++;
      mat num = getNum();
      st.push(num);
    } else if (s[p] == '-') {
      p++;
      mat num = getNum();
      for (int i = 0; i < (int)num.size(); i++)
        num[i].first *= -1;
      st.push(num);

    } else if (s[p] == ')') {
      break;
    } else {
      mat num = getNum();
      mat A = st.top();
      st.pop();
      st.push(mul(A, num));
    }
  }
  mat res = st.top();
  st.pop();
  while (!st.empty()) {
    res = add(res, st.top());
    st.pop();
  }

  for (int i = 0; i < (int)res.size(); i++) {
    sort(res[i].second.begin(), res[i].second.end());
  }
  sort(res.begin(), res.end());
  return res;
}

int main() {
  while (1) {
    getline(cin, s);
    if (s == ".")
      break;
    p = 0;
    len = s.size();
    target = solve();

    while (1) {
      getline(cin, s);
      if (s == ".")
        break;
      p = 0;
      len = s.size();
      mat tmp = solve();
      if (target == tmp)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
    cout << '.' << endl;
  }
  return 0;
}