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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int W, H;
char field[70][70];

const int dx[] = {1, 0}, dy[] = {0, 1};

string maxstr(const string &s1, const string &s2) {
  if (s1.length() == s2.length()) {
    if (s1 > s2) {
      return s1;
    } else {
      return s2;
    }
  } else {
    if (s1.length() > s2.length()) {
      return s1;
    } else {
      return s2;
    }
  }
}

void replace(string &s) {
  while (s.find("0") == 0)
    s.replace(0, 1, "");
}

string maxstr0(string s1, string s2) {
  replace(s1);
  replace(s2);

  if (s1.length() == s2.length()) {
    if (s1 > s2) {
      return s1;
    } else {
      return s2;
    }
  } else {
    if (s1.length() > s2.length()) {
      return s1;
    } else {
      return s2;
    }
  }
}

int main() {
  while (cin >> W >> H, W | H) {
    REP(y, H) REP(x, W) { cin >> field[y][x]; }

    vector<vector<string>> s(H, vector<string>(W));

    string ma = "";
    for (int y = H - 1; y >= 0; y--)
      for (int x = W - 1; x >= 0; x--) {
        if (isdigit(field[y][x])) {
          REP(i, 2) {
            int sx = x + dx[i], sy = y + dy[i];
            if (0 <= sx && sx < W && 0 <= sy && sy < H &&
                isdigit(field[sy][sx])) {
              s[y][x] = maxstr(s[y][x], field[y][x] + s[sy][sx]);
            }
          }
          if (s[y][x].length() == 0) {
            s[y][x] = field[y][x];
          }
          ma = maxstr0(ma, s[y][x]);
        }
      }

    cout << ma << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int W, H;
char field[70][70];
bool seen[70][70];
bool number[70][70];
vector<string> v;

const int dx[] = {1, 0}, dy[] = {0, 1};

void dfs(int x, int y, vector<char> &cs) {
  seen[y][x] = true;
  bool noway = true;
  REP(i, 2) {
    int sx = x + dx[i], sy = y + dy[i];
    if (0 <= sx && sx < W && 0 <= sy && sy < H && number[sy][sx]) {
      vector<char> copy(cs);
      copy.push_back(field[sy][sx]);
      dfs(sx, sy, copy);
      noway = false;
    }
  }
  if (noway) {
    // ?????????0?????????
    for (auto it = cs.begin(); it != cs.end();) {
      if (*it == '0') {
        it = cs.erase(it);
      } else {
        break;
      }
    }
    string s = string(ALL(cs));
    if (SZ(v) == 0) {
      v.push_back(s);
    } else if (v[0].length() == s.length()) {
      v.push_back(s);
    } else if (v[0].length() < s.length()) {
      v.clear();
      v.push_back(s);
    }
    //        LOG("--> %s\\n", s.c_str());
  }
}

int main() {
  while (cin >> W >> H, W | H) {
    v.clear();
    fill_n((bool *)seen, 70 * 70, false);
    fill_n((bool *)number, 70 * 70, false);
    REP(y, H) REP(x, W) {
      cin >> field[y][x];
      if (isdigit(field[y][x])) {
        number[y][x] = true;
      }
    }
    REP(y, H) REP(x, W) {
      if (number[y][x] && !seen[y][x]) {
        vector<char> cs{field[y][x]};
        dfs(x, y, cs);
      }
    }
    RSORT(v);
    cout << v[0] << endl;
  }
}