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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-7;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

vector<vector<ll>> rnd(100, vector<ll>(26));
int p, var;

ll sum(string &s);

ll num(string &s) {
  ll res = 0;
  while (isdigit(s[p])) {
    res *= 10;
    res += s[p] - '0';
    p++;
  }
  return res;
}

ll unit(string &s) {
  ll res;
  while (s[p] == ' ')
    p++;
  if (s[p] == '(') {
    p++;
    res = sum(s);
    p++;
  } else if (isdigit(s[p]))
    res = num(s);
  else if (islower(s[p])) {
    res = rnd[var][s[p] - 'a'];
    p++;
  } else
    assert(false);
  return res;
}

ll pow(string &s) {
  ll lhs = unit(s);
  while (s[p] == '^') {
    p++;
    while (s[p] == ' ')
      p++;
    ll rhs = pow(s);
    ll tmp = 1;
    REP(i, rhs) { tmp *= lhs; }
    lhs = tmp;
  }
  return lhs;
}

ll mul(string &s) {
  ll lhs = pow(s);
  while (s[p] == '*') {
    p++;
    ll rhs;
    rhs = pow(s);
    lhs *= rhs;
  }
  return lhs;
}

ll sum(string &s) {
  ll lhs = mul(s);
  while (s[p] == '+' || s[p] == '-') {
    if (s[p] == '+') {
      p++;
      ll rhs = mul(s);
      lhs += rhs;
    } else {
      p++;
      ll rhs = mul(s);
      lhs -= rhs;
    }
  }
  return lhs;
}

ll parse(string &s) {
  p = 0;
  return sum(s);
}

bool isMulti(char a, char b, bool flag) {
  if (flag && isdigit(a) && isdigit(b))
    return true;
  if (isdigit(a) && islower(b))
    return true;
  if (islower(a) && islower(b))
    return true;
  if (islower(a) && isdigit(b))
    return true;
  if ((a == ')') && (b == '('))
    return true;
  if ((a == ')') && (isdigit(b) || islower(b)))
    return true;
  if ((isdigit(a) || islower(a)) && (b == '('))
    return true;
  return false;
}

string make(string s) {
  string tmp, res;
  stringstream ss;
  ss << s;
  vector<string> buf;
  while (!(ss >> s).fail())
    buf.push_back(s);
  REP(i, buf.size()) {
    if (i + 1 < buf.size() && isMulti(buf[i].back(), buf[i + 1][0], true)) {
      tmp += (buf[i] + "*");
    } else
      tmp += buf[i];
  }
  REP(i, tmp.size()) {
    if (i + 1 < tmp.size() && isMulti(tmp[i], tmp[i + 1], false))
      res += string(1, tmp[i]) + "*";
    else
      res += string(1, tmp[i]);
  }
  return res;
}

int main() {
  string s, t;
  REP(i, 100) REP(j, 26) rnd[i][j] = rand() % 20;
  while (getline(cin, s), s != ".") {
    string s_ = make(s);
    while (getline(cin, t), t != ".") {
      string t_ = make(t);
      bool f = true;
      for (var = 0; var < 100 && f; var++) {
        ll lhs = parse(s_);
        ll rhs = parse(t_);
        if (lhs != rhs)
          f = false;
      }
      puts(f ? "yes" : "no");
    }
    puts(".");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-7;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

vector<vector<ll>> rnd(10, vector<ll>(26));
int p, var;

ll sum(string &s);

ll num(string &s) {
  ll res = 0;
  while (isdigit(s[p])) {
    res *= 10;
    res += s[p] - '0';
    p++;
  }
  return res;
}

ll unit(string &s) {
  ll res;
  while (s[p] == ' ')
    p++;
  if (s[p] == '(') {
    p++;
    res = sum(s);
    p++;
  } else if (isdigit(s[p]))
    res = num(s);
  else if (islower(s[p])) {
    res = rnd[var][s[p] - 'a'];
    p++;
  } else
    assert(false);
  return res;
}

ll pow(string &s) {
  ll lhs = unit(s);
  while (s[p] == '^') {
    p++;
    while (s[p] == ' ')
      p++;
    ll rhs = pow(s);
    ll tmp = 1;
    REP(i, rhs) { tmp *= lhs; }
    lhs = tmp;
  }
  return lhs;
}

ll mul(string &s) {
  ll lhs = pow(s);
  while (s[p] == '*' || isdigit(s[p]) || islower(s[p])) {
    while (s[p] == '*')
      p++;
    ll rhs;
    rhs = pow(s);
    lhs *= rhs;
  }
  return lhs;
}

ll sum(string &s) {
  ll lhs = mul(s);
  while (s[p] == '+' || s[p] == '-') {
    if (s[p] == '+') {
      p++;
      ll rhs = mul(s);
      lhs += rhs;
    } else {
      p++;
      ll rhs = mul(s);
      lhs -= rhs;
    }
  }
  return lhs;
}

ll parse(string &s) {
  p = 0;
  return sum(s);
}

bool isop(char c) { return !(c != '+' && c != '-' && c != '^' && c != '*'); }

string make(string s) {
  string res;
  REP(i, s.size()) {
    if (s[i] == ' ') {
      char c = res.back();
      while (s[i] == ' ')
        i++;
      char d = s[i];
      if (isop(c) || isop(d))
        res.push_back(d);
      else {
        res.push_back('*');
        res.push_back(d);
      }
    } else {
      res.push_back(s[i]);
    }
  }
  return res;
}

int main() {
  string s, t;
  REP(i, 10) REP(j, 26) rnd[i][j] = rand() % 20;
  while (getline(cin, s), s != ".") {
    string s_ = make(s);
    while (getline(cin, t), t != ".") {
      string t_ = make(t);
      bool f = true;
      for (var = 0; var < 10 && f; var++) {
        ll lhs = parse(s_);
        ll rhs = parse(t_);
        if (lhs != rhs)
          f = false;
      }
      puts(f ? "yes" : "no");
    }
    puts(".");
  }
  return 0;
}