Problem p00707 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cctype>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define MAX 72
#define INV (-1)

struct Dig {
  vector<char> s;
  void push(char c) { s.push_back(c); }
  void pop() { s.pop_back(); }
  void simplify() {
    while (true) {
      if (s.size() == 1)
        break;
      if (s[0] == '0') {
        s.erase(s.begin());
      } else
        break;
    }
  }
  bool operator<(const Dig &t) {
    if (s.size() < t.s.size())
      return true;
    else if (s.size() == t.s.size()) {
      for (unsigned int i = 0; i < s.size(); ++i) {
        if (s[i] == t.s[i])
          continue;
        if (s[i] < t.s[i])
          return true;
        else
          return false;
      }
    }
    return false;
  }
};

vector<Dig> vdigs;
void ResetMap(char map[MAX][MAX]) {
  for (int i = 0; i < MAX; ++i) {
    for (int j = 0; j < MAX; ++j) {
      map[i][j] = INV;
    }
  }
}
template <typename _T> ostream &operator<<(ostream &os, const vector<_T> &v) {
  for (unsigned int i = 0; i < v.size(); ++i) {
    os << v[i];
  }
  return os;
}
int main() {
  while (true) {
    int H, W;
    Dig ans;
    char map[MAX][MAX];
    Dig dmap[MAX][MAX];

    cin >> W >> H;

    if (W == 0 && H == 0)
      break;
    vdigs.clear();
    ResetMap(map);
    for (int i = 1; i <= H; ++i) {
      string s;
      cin >> s;
      for (int j = 1; j <= W; ++j) {
        map[i][j] = s[j - 1];
      }
    }
    for (int i = 1; i <= H; ++i) {
      for (int j = 1; j <= W; ++j) {
        if (!isdigit(map[i][j]))
          continue;
        else {
          Dig d, d1, d2;
          Dig dmax;
          d.push(map[i][j]);
          if (isdigit(map[i - 1][j])) {
            d1 = dmap[i - 1][j];
            d1.push(map[i][j]);
          }
          if (isdigit(map[i][j - 1])) {
            d2 = dmap[i][j - 1];
            d2.push(map[i][j]);
          }
          dmax = d;
          if (dmax < d1)
            dmax = d1;
          if (dmax < d2)
            dmax = d2;
          dmax.simplify();
          dmap[i][j] = dmax;
        }
      }
    }
    for (int i = 1; i <= H; ++i) {
      for (int j = 1; j <= W; ++j) {
        if (ans < dmap[i][j]) {
          ans = dmap[i][j];
        }
      }
    }
    cout << ans.s << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cctype>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define MAX 72
#define INV (-1)

struct Dig {
  vector<char> s;
  vector<pair<int, int>> pos;
  void push(char c, int i, int j) {
    s.push_back(c);
    pos.push_back(make_pair(i, j));
  }
  void pop() {
    s.pop_back();
    pos.pop_back();
  }
  void simplify() {
    while (true) {
      if (s.size() == 1)
        break;
      if (s[0] == '0') {
        s.erase(s.begin());
      } else
        break;
    }
  }
  bool operator<(const Dig &t) {
    if (s.size() < t.s.size())
      return true;
    else if (s.size() == t.s.size()) {
      for (unsigned int i = 0; i < s.size(); ++i) {
        if (s[i] < t.s[i])
          return true;
        else
          return false;
      }
    }
    return false;
  }
};

vector<Dig> vdigs;

void dfs(char map[MAX][MAX], int i, int j, Dig &d) {
  const int di[] = {1, 0};
  const int dj[] = {0, 1};
  if (!isdigit(map[i][j]))
    return;
  bool bCanGoNext = false;
  for (int k = 0; k < 2; ++k) {
    if (isdigit(map[i + di[k]][j + dj[k]])) {
      d.push(map[i][j], i, j);
      bCanGoNext = true;
      dfs(map, i + di[k], j + dj[k], d);
      d.pop();
    }
  }
  if (!bCanGoNext) {
    d.push(map[i][j], i, j);
    vdigs.push_back(d);
    d.pop();
  }
}
void ResetMap(char map[MAX][MAX]) {
  for (int i = 0; i < MAX; ++i) {
    for (int j = 0; j < MAX; ++j) {
      map[i][j] = INV;
    }
  }
}
template <typename _T> ostream &operator<<(ostream &os, const vector<_T> &v) {
  for (unsigned int i = 0; i < v.size(); ++i) {
    os << v[i];
  }
  return os;
}

int LCS(const Dig &a, const Dig &b) {
  int ret = 0;
  int table[MAX][MAX] = {
      {
          0,
      },
  };
  for (unsigned int i = 1; i <= a.pos.size(); ++i) {
    for (unsigned int j = 1; j <= b.pos.size(); ++j) {
      if (a.pos[i - 1] == b.pos[j - 1]) {
        table[i][j] = 1 + table[i - 1][j - 1];
      } else {
        table[i][j] = max(table[i - 1][j], table[i][j - 1]);
      }
      ret = max(ret, table[i][j]);
    }
  }
  return ret;
}

int main() {
  while (true) {
    int max_lcs = 0;
    vector<vector<int>> lcs_table;
    int H, W;
    Dig d;
    Dig ans;
    char map[MAX][MAX];

    cin >> W >> H;

    if (W == 0 && H == 0)
      break;
    vdigs.clear();
    ResetMap(map);
    for (int i = 1; i <= H; ++i) {
      string s;
      cin >> s;
      for (int j = 1; j <= W; ++j) {
        map[i][j] = s[j - 1];
      }
    }
    for (int i = 1; i <= H; ++i) {
      for (int j = 1; j <= W; ++j) {
        if (isdigit(map[i - 1][j]) || isdigit(map[i][j - 1]))
          continue;
        dfs(map, i, j, d);
      }
    }

    for (unsigned int i = 0; i < vdigs.size(); ++i) {
      vdigs[i].simplify();
    }

    for (unsigned int i = 0; i < vdigs.size(); ++i) {
      vector<int> lcss;
      for (unsigned int j = i + 1; j < vdigs.size(); ++j) {
        int t = LCS(vdigs[i], vdigs[j]);
        lcss.push_back(t);
        max_lcs = max(max_lcs, t);
      }
      lcs_table.push_back(lcss);
    }

    ans.s.push_back('0');
    for (unsigned int i = 0; i < vdigs.size(); ++i) {
      for (unsigned int j = i + 1; j < vdigs.size(); ++j) {
        if (max_lcs == lcs_table[i][j - i - 1]) {
          if (vdigs[i] < vdigs[j]) {
            if (ans < vdigs[j])
              ans = vdigs[j];
          } else {
            if (ans < vdigs[i])
              ans = vdigs[i];
          }
        }
      }
    }
    for (unsigned int i = 0; i < vdigs.size(); ++i) {
      vdigs[i].s.clear();
      vdigs[i].pos.clear();
    }
    cout << ans.s << endl;
  }
  return 0;
}