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

#include <algorithm>
#include <array>
#include <cctype>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

// mat[W][H]

std::string::iterator end;

class Matrix {
public:
  void show() {
    for (auto y = 0; y != (*m_Mat)[0].size(); ++y) {
      for (auto x = 0; x != (*m_Mat).size(); ++x) {
        std::cout << (32768 + (*m_Mat)[x][y] % 32768) % 32768;

        if (x + 1 != (*m_Mat).size()) {
          std::cout << " ";
        }
      }
      std::cout << std::endl;
    }
  }

  Matrix() { m_Mat = std::make_shared<std::vector<std::vector<long long>>>(); }
  Matrix(long long i) {
    m_Mat = std::make_shared<std::vector<std::vector<long long>>>();
    m_Mat->push_back(std::vector<long long>());
    (*m_Mat)[0].push_back(i);
  }

  Matrix clone() {
    Matrix mat;
    *mat.m_Mat = *m_Mat;
    return mat;
  }

  void newLines(Matrix mat) {
    for (int x = 0; x < mat.m_Mat->size(); ++x) {
      for (int y = 0; y < (*mat.m_Mat)[0].size(); ++y) {
        (*m_Mat)[x].push_back((*mat.m_Mat)[x][y]);
      }
    }
  }
  void newColums(Matrix mat) {
    for (auto it = mat.m_Mat->begin(); it != mat.m_Mat->end(); ++it) {
      m_Mat->push_back(*it);
    }
  }
  bool isInt() { return m_Mat->size() == 1 && (*m_Mat)[0].size() == 1; }

  Matrix &operator+=(Matrix mat) {
    for (auto w = 0; w < m_Mat->size(); ++w) {
      for (auto h = 0; h < (*m_Mat)[w].size(); ++h) {
        (*m_Mat)[w][h] += (*mat.m_Mat)[w][h];
      }
    }
    return *this;
  }

  Matrix &operator-=(Matrix mat) {
    for (auto w = 0; w < m_Mat->size(); ++w) {
      for (auto h = 0; h < (*m_Mat)[0].size(); ++h) {
        (*m_Mat)[w][h] -= (*mat.m_Mat)[w][h];
      }
    }
    return *this;
  }

  Matrix &operator*=(Matrix mat) {
    if (mat.isInt()) {
      *this *= (*mat.m_Mat)[0][0];
      return *this;
    } else if (isInt()) {
      mat *= (*this->m_Mat)[0][0];
      this->m_Mat = mat.m_Mat;
      return *this;
    }

    Matrix newMat;
    for (auto r = 0; r < mat.m_Mat->size(); ++r) {
      (*newMat.m_Mat).push_back(std::vector<long long>());
      for (auto l = 0; l < (*m_Mat)[0].size(); ++l) {
        (*newMat.m_Mat)[r].push_back(0);
        for (auto i = 0; i < m_Mat->size(); ++i) { //
          (*newMat.m_Mat)[r][l] += (*m_Mat)[i][l] * (*mat.m_Mat)[r][i];
        }
      }
    }

    this->m_Mat = newMat.m_Mat;

    return *this;
  }

  Matrix &operator*=(int x) {
    for (auto w = 0; w < m_Mat->size(); ++w) {
      for (auto h = 0; h < (*m_Mat)[0].size(); ++h) {
        (*m_Mat)[w][h] *= x;
      }
    }
    return *this;
  }

  Matrix &indexed(Matrix h, Matrix w) {
    Matrix mat;
    for (auto x = (*w.m_Mat).begin(); x != (*w.m_Mat).end(); ++x) {
      mat.m_Mat->push_back(std::vector<long long>());
      for (auto y = (*h.m_Mat).begin(); y != (*h.m_Mat).end(); ++y) {
        (*mat.m_Mat)[mat.m_Mat->size() - 1].push_back(
            (*m_Mat)[(*x)[0] - 1][(*y)[0] - 1]);
      }
    }
    this->m_Mat = mat.m_Mat;
    return *this;
  }

  Matrix &trans() {
    Matrix mat;

    for (int x = 0; x < (*m_Mat)[0].size(); ++x) {
      mat.m_Mat->push_back(std::vector<long long>());
      for (int y = 0; y < m_Mat->size(); ++y) {
        (*mat.m_Mat)[mat.m_Mat->size() - 1].push_back((*m_Mat)[y][x]);
      }
    }
    m_Mat = mat.m_Mat;
    return *this;
  }

private:
  std::shared_ptr<std::vector<std::vector<long long>>> m_Mat;
};

std::array<Matrix, 27> g_Dict;

Matrix parseExpr(std::string::iterator &it);

// --------------------------------

Matrix parseInum(std::string::iterator &it) {
  while (isdigit(*it))
    ++it;
  auto tmp = it;

  --it;
  unsigned int k = 1;
  unsigned int sum = 0;
  while (isdigit(*it)) {
    sum += k * ((*it) - '0');
    k *= 10;
    --it;
  }

  it = tmp;
  return Matrix(sum);
}

Matrix parseRow(std::string::iterator &it) {
  auto mat = parseExpr(it);
  while (*it == ' ') {
    ++it;

    auto rMat = parseExpr(it);
    mat.newColums(rMat);
  }

  return mat;
}

Matrix parseRowSeq(std::string::iterator &it) {
  auto mat = parseRow(it);

  while (*it == ';') {
    ++it;
    auto rMat = parseRow(it);
    mat.newLines(rMat);
  }
  return mat;
}

Matrix parseMatrix(std::string::iterator &it) {
  ++it; // [
  auto mat = parseRowSeq(it);
  ++it; // ]

  return mat;
}

Matrix parsePrimary(std::string::iterator &it) {
  Matrix mat;
  if (isalpha(*it)) {
    mat = g_Dict[*it - 'A'].clone();
    ++it;
  } else if (isdigit(*it)) {
    mat = parseInum(it);
  } else if (*it == '(') {
    ++it;
    mat = parseExpr(it);
    ++it; // )
  } else if (*it == '[') {
    mat = parseMatrix(it);
  }

  while (true) {
    if (*it == '\\'') {
      mat.trans();
      ++it;
    } else if (*it == '(') {
      ++it;
      auto h = parseExpr(it);
      ++it; // ,
      auto w = parseExpr(it);
      ++it; // )
      mat.indexed(h, w);
    } else {
      break;
    }
  }

  return mat;
}

Matrix parseFactor(std::string::iterator &it) {
  if (*it == '-') {
    ++it;
    auto mat = parseFactor(it);
    mat *= -1;

    return mat;
  } else {
    return parsePrimary(it);
  }
}

Matrix parseTerm(std::string::iterator &it) {
  auto mat = parseFactor(it);

  while (*it == '*') {
    ++it;
    auto rMat = parseFactor(it);
    mat *= rMat;
  }

  return mat;
}

Matrix parseExpr(std::string::iterator &it) {
  auto mat = parseTerm(it);

  while (*it == '-' || *it == '+') {
    char op = *it;
    ++it;

    auto rMat = parseTerm(it);
    if (op == '-') {
      mat -= rMat;
    } else {
      mat += rMat;
    }
  }

  return mat;
}

Matrix parseAssignment(std::string::iterator &it) {
  char currentVar = *it;
  ++it;
  ++it; // A=

  g_Dict[currentVar - 'A'] = parseExpr(it);

  ++it; // .
  return g_Dict[currentVar - 'A'];
}

// auto in = freopen("input", "r", stdin);

int main() {
  while (true) {
    int m;
    std::cin >> m;
    if (m == 0)
      return 0;
    std::string str;
    std::getline(std::cin, str);
    for (int i = 0; i < m; ++i) {
      // std::string str;
      std::getline(std::cin, str);
      auto it = str.begin();
      end = str.end();
      parseAssignment(it).show();
    }

    std::cout << "-----" << std::endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <array>
#include <cctype>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

// mat[W][H]

std::string::iterator end;

class Matrix {
public:
  void show() {
    for (auto y = 0; y != (*m_Mat)[0].size(); ++y) {
      for (auto x = 0; x != (*m_Mat).size(); ++x) {
        std::cout << (32768 + (*m_Mat)[x][y] % 32768) % 32768;

        if (x + 1 != (*m_Mat).size()) {
          std::cout << " ";
        }
      }
      std::cout << std::endl;
    }
  }

  Matrix() { m_Mat = std::make_shared<std::vector<std::vector<long long>>>(); }
  Matrix(long long i) {
    m_Mat = std::make_shared<std::vector<std::vector<long long>>>();
    m_Mat->push_back(std::vector<long long>());
    (*m_Mat)[0].push_back(i);
  }

  Matrix clone() {
    Matrix mat;
    *mat.m_Mat = *m_Mat;
    return mat;
  }

  void newLines(Matrix mat) {
    for (int x = 0; x < mat.m_Mat->size(); ++x) {
      for (int y = 0; y < (*mat.m_Mat)[0].size(); ++y) {
        (*m_Mat)[x].push_back((*mat.m_Mat)[x][y]);
      }
    }
  }
  void newColums(Matrix mat) {
    for (auto it = mat.m_Mat->begin(); it != mat.m_Mat->end(); ++it) {
      m_Mat->push_back(*it);
    }
  }
  bool isInt() { return m_Mat->size() == 1 && (*m_Mat)[0].size() == 1; }

  Matrix &operator+=(Matrix mat) {
    for (auto w = 0; w < m_Mat->size(); ++w) {
      for (auto h = 0; h < (*m_Mat)[w].size(); ++h) {
        (*m_Mat)[w][h] += (*mat.m_Mat)[w][h];
      }
    }
    return *this;
  }

  Matrix &operator-=(Matrix mat) {
    for (auto w = 0; w < m_Mat->size(); ++w) {
      for (auto h = 0; h < (*m_Mat)[0].size(); ++h) {
        (*m_Mat)[w][h] -= (*mat.m_Mat)[w][h];
      }
    }
    return *this;
  }

  Matrix &operator*=(Matrix mat) {
    if (mat.isInt()) {
      *this *= (*mat.m_Mat)[0][0];
      return *this;
    } else if (isInt()) {
      mat *= (*this->m_Mat)[0][0];
      this->m_Mat = mat.m_Mat;
      return *this;
    }

    Matrix newMat;
    for (auto r = 0; r < mat.m_Mat->size(); ++r) {
      (*newMat.m_Mat).push_back(std::vector<long long>());
      for (auto l = 0; l < (*m_Mat)[0].size(); ++l) {
        (*newMat.m_Mat)[r].push_back(0);
        for (auto i = 0; i < m_Mat->size(); ++i) { //
          (*newMat.m_Mat)[r][l] += (*m_Mat)[i][l] * (*mat.m_Mat)[r][i];
        }
      }
    }

    this->m_Mat = newMat.m_Mat;

    return *this;
  }

  Matrix &operator*=(int x) {
    for (auto w = 0; w < m_Mat->size(); ++w) {
      for (auto h = 0; h < (*m_Mat)[0].size(); ++h) {
        (*m_Mat)[w][h] *= x;
      }
    }
    return *this;
  }

  Matrix &indexed(Matrix h, Matrix w) {
    Matrix mat;
    for (auto x = (*w.m_Mat).begin(); x != (*w.m_Mat).end(); ++x) {
      mat.m_Mat->push_back(std::vector<long long>());
      for (auto y = (*h.m_Mat).begin(); y != (*h.m_Mat).end(); ++y) {
        (*mat.m_Mat)[mat.m_Mat->size() - 1].push_back(
            (*m_Mat)[(*x)[0] - 1][(*y)[0] - 1]);
      }
    }
    this->m_Mat = mat.m_Mat;
    return *this;
  }

  Matrix &trans() {
    Matrix mat;

    for (int x = 0; x < (*m_Mat)[0].size(); ++x) {
      mat.m_Mat->push_back(std::vector<long long>());
      for (int y = 0; y < m_Mat->size(); ++y) {
        (*mat.m_Mat)[mat.m_Mat->size() - 1].push_back((*m_Mat)[y][x]);
      }
    }
    m_Mat = mat.m_Mat;
    return *this;
  }

private:
  std::shared_ptr<std::vector<std::vector<long long>>> m_Mat;
};

std::array<Matrix, 27> g_Dict;

Matrix parseExpr(std::string::iterator &it);

// --------------------------------

Matrix parseInum(std::string::iterator &it) {
  while (isdigit(*it))
    ++it;
  auto tmp = it;

  --it;
  unsigned int k = 1;
  unsigned int sum = 0;
  while (isdigit(*it)) {
    sum += k * ((*it) - '0');
    k *= 10;
    --it;
  }

  it = tmp;
  return Matrix(sum);
}

Matrix parseRow(std::string::iterator &it) {
  auto mat = parseExpr(it);
  while (*it == ' ') {
    ++it;

    auto rMat = parseExpr(it);
    mat.newColums(rMat);
  }

  return mat;
}

Matrix parseRowSeq(std::string::iterator &it) {
  auto mat = parseRow(it);

  while (*it == ';') {
    ++it;
    auto rMat = parseRow(it);
    mat.newLines(rMat);
  }
  return mat;
}

Matrix parseMatrix(std::string::iterator &it) {
  ++it; // [
  auto mat = parseRowSeq(it);
  ++it; // ]

  return mat;
}

Matrix parsePrimary(std::string::iterator &it) {
  Matrix mat;
  if (isalpha(*it)) {
    mat = g_Dict[*it - 'A'].clone();
    ++it;
  } else if (isdigit(*it)) {
    mat = parseInum(it);
  } else if (*it == '(') {
    ++it;
    mat = parseExpr(it);
    ++it; // )
    ++it;
  } else if (*it == '[') {
    mat = parseMatrix(it);
  }

  if (*it == '\\'') {
    mat.trans();
  } else if (*it == '(') {
    ++it;
    auto h = parseExpr(it);
    ++it; // ,
    auto w = parseExpr(it);
    ++it; // )
    mat.indexed(h, w);
  }

  return mat;
}

Matrix parseFactor(std::string::iterator &it) {
  if (*it == '-') {
    ++it;
    auto mat = parseFactor(it);
    mat *= -1;

    return mat;
  } else {
    return parsePrimary(it);
  }
}

Matrix parseTerm(std::string::iterator &it) {
  auto mat = parseFactor(it);

  while (*it == '*') {
    ++it;
    auto rMat = parseFactor(it);
    mat *= rMat;
  }

  return mat;
}

Matrix parseExpr(std::string::iterator &it) {
  auto mat = parseTerm(it);

  while (*it == '-' || *it == '+') {
    char op = *it;
    ++it;

    auto rMat = parseTerm(it);
    if (op == '-') {
      mat -= rMat;
    } else {
      mat += rMat;
    }
  }

  return mat;
}

Matrix parseAssignment(std::string::iterator &it) {
  char currentVar = *it;
  ++it;
  ++it; // A=

  g_Dict[currentVar - 'A'] = parseExpr(it);

  ++it; // .
  return g_Dict[currentVar - 'A'];
}

// auto in = freopen("input", "r", stdin);

int main() {
  while (true) {
    int m;
    std::cin >> m;
    if (m == 0)
      return 0;
    std::string str;
    std::getline(std::cin, str);
    for (int i = 0; i < m; ++i) {
      // std::string str;
      std::getline(std::cin, str);
      auto it = str.begin();
      end = str.end();
      parseAssignment(it).show();
    }

    std::cout << "-----" << std::endl;
  }
}