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

#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

string S;
map<string, int> M;
int size;
int pos = 0;
vector<vector<int>> V;

bool rev;

int gcd(int a, int b) { return a % b ? gcd(b, a % b) : b; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

vector<int> read() {
  vector<int> res(size, 0);
  if (S[pos] == '+' || S[pos] == ')' || S[pos] == '-') {
    pos++;
    return res;
  }
  if (S[pos] == '.') {
    return res;
  }

  vector<int> now(size, 0);
  string s = "";
  if (S[pos] == '(') {
    pos++;
    res = read();
  } else {
    s += S[pos];
    if (islower(S[pos + 1]))
      s += S[++pos];
    pos++;
    res[M[s] - 1] = 1;
  }
  int num = 1;
  if (isdigit(S[pos])) {
    num = S[pos] - '0';
    if (isdigit(S[pos + 1])) {
      num *= 10;
      num += S[++pos] - '0';
    }
    pos++;
  }
  for (int i = 0; i < res.size(); i++)
    res[i] *= num;
  vector<int> next = read();
  for (int i = 0; i < next.size(); i++)
    res[i] += next[i];
  return res;
}

void print() {

  for (int y = 0; y < V.size(); y++) {
    for (int x = 0; x < V[y].size(); x++)
      cout << V[y][x] << " ";
    cout << endl;
  }
  cout << endl;
}

void solve() {
  size = 0;
  M.clear();
  pos = 0;
  V.clear();
  rev = false;
  while (S[pos] != '.') {
    string s;
    if (!isalpha(S[pos])) {
      pos++;
      continue;
    }
    s += S[pos];
    if (islower(S[pos + 1])) {
      s += S[pos + 1];
      pos++;
    }
    pos++;
    if (M[s] == 0) {
      M[s] = ++size;
    }
  }
  V.resize(size);
  pos = 0;
  while (S[pos] != '.') {
    if (S[pos] == '>') {
      rev = true;
      pos++;
    }
    vector<int> res = read();
    for (int i = 0; i < res.size(); i++)
      V[i].push_back(rev ? -res[i] : res[i]);
  }
  // verifyed!

  for (int i = 0; i < V[0].size() - 1; i++) {
    if (V[i][i] == 0) {
      int z = i + 1;
      while (V[z][i] == 0)
        z++;
      swap(V[i], V[z]);
    }
    if (V[i][i] < 0) {
      for (int z = 0; z < V[i].size(); z++)
        V[i][z] *= -1;
    }

    for (int j = 0; j < V.size(); j++) {
      if (V[j][i] == 0 || i == j)
        continue;
      if (V[j][i] < 0)
        for (int z = 0; z < V[j].size(); z++)
          V[j][z] *= -1;
      int l = lcm(V[i][i], V[j][i]);
      int mul = l / V[i][i];
      for (int z = 0; z < V[i].size(); z++)
        V[i][z] *= mul;
      mul = l / V[j][i];
      for (int z = 0; z < V[j].size(); z++)
        V[j][z] *= mul;
      for (int z = 0; z < V[i].size(); z++)
        V[j][z] -= V[i][z];
    }
    for (int j = 0; j < V.size(); j++) {
      int l = 0;
      for (int z = 0; z < V[j].size(); z++) {
        if (V[j][z] == 0)
          continue;
        if (l == 0)
          l = abs(V[j][z]);
        else
          l = gcd(abs(V[j][z]), l);
      }
      if (l != 0) {
        for (int z = 0; z < V[j].size(); z++)
          V[j][z] /= l;
      }
    }
  }

  int l = abs(V[0][0]);
  for (int i = 0; i < V[0].size() - 1; i++)
    l = lcm(l, abs(V[i][i]));

  for (int i = 0; i < V[0].size() - 1; i++) {
    int mul = l / abs(V[i][i]);
    cout << mul * abs(V[i][V[i].size() - 1]) << " ";
  }

  cout << l << endl;
  ;
}

int main() {
  while (cin >> S) {
    if (S == ".")
      break;
    solve();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

string str;
map<string, int> Idx;
int atom = 0;
int pos = 0;
int num = 0;
bool rev;

vector<int> read() {
  vector<int> res;
  if (str.size() <= pos || str[pos] == '+' || str[pos] == '.' ||
      str[pos] == ')' || str[pos] == '-') {
    return res;
  }
  string temp = "";
  if (isupper(str[pos])) {
    temp += str[pos++];
    if (islower(str[pos]))
      temp += str[pos++];
    res[Idx[temp]] += 1;
  } else if (str[pos] == '(') {
    pos++;
    res = read();
    pos++;
  }
  int mul = 1;
  if (isdigit(str[pos])) {
    mul = str[pos++] - '0';
    if (isdigit(str[pos]))
      mul = mul * 10 + str[pos++] - '0';
  }
  vector<int> next;
  next = read();
  for (int i = 0; i < res.size(); i++)
    res[i] = res[i] * mul + next[i];
  return res;
}

int gcd(int a, int b) { return a % b ? gcd(b, a % b) : b; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main() {
  ifstream fin("F.in");
  while (cin >> str) {
    if (str == ".")
      break;
    atom = 0;
    pos = 0;
    num = 2;
    rev = false;
    Idx.clear();
    for (int i = 0; i < str.size(); i++) {
      string temp = "";
      if (str[i] == '+')
        num++;
      if (isupper(str[i])) {
        temp += str[i];
        if (i + 1 < str.size() && islower(str[i + 1]))
          temp += str[++i];
      }
      if (temp != "" && !Idx.count(temp))
        Idx[temp] = atom++;
    }
    vector<vector<int>> v;
    for (int i = 0; i < num; i++) {
      if (str[pos] == '+')
        pos++;
      if (str[pos] == '-') {
        pos += 2;
        rev = true;
      }
      vector<int> temp = read();
      for (int j = 0; j < temp.size(); j++)
        v[j].push_back(!rev ? temp[j] : -temp[j]);
    }
    for (int i = 0; i < v[0].size() - 1; i++) {
      if (v[i][i] == 0) {
        int idx = i;
        while (!v[idx][i])
          idx++;
        swap(v[i], v[idx]);
      }
      for (int j = 0; j < v.size(); j++) {
        if (v[j][i] == 0 || i == j)
          continue;
        int mula = v[i][i] / gcd(v[i][i], v[j][i]);
        int mulb = v[j][i] / gcd(v[i][i], v[j][i]);
        for (int k = 0; k < v[0].size(); k++)
          v[j][k] = v[j][k] * mula - v[i][k] * mulb;
      }
      for (int j = 0; j < v.size(); j++) {
        int div = 0;
        for (int k = 0; k < v[0].size(); k++)
          if (v[j][k] != 0)
            div = !div ? abs(v[j][k]) : gcd(abs(v[j][k]), div);
        if (div)
          for (int k = 0; k < v[0].size(); k++)
            v[j][k] /= div;
      }
    }

    int mul = 1;
    for (int i = 0; i < num - 1; i++)
      mul = lcm(mul, abs(v[i][i]));
    for (int i = 0; i < num - 1; i++)
      cout << abs(v[i][num - 1] * mul / v[i][i]) << " ";
    cout << mul << endl;
  }
  return 0;
}