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

#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
// #define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c), end(c)
// #define EXIST(s, e) ((s).find(e)!=(s).end())
// #define SORT(c) sort(begin(c),end(c))
// #define pb emplace_back
// #define MP make_pair
// #define SZ(a) int((a).size())

// #define LOCAL 0
// #ifdef LOCAL
// #define DEBUG(s) cout << (s) << endl
// #define dump(x)  cerr << #x << " = " << (x) << endl
// #define BR cout << endl;
// #else
// #define DEBUG(s) do{}while(0)
// #define dump(x) do{}while(0)
// #define BR
// #endif

// 改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 // デバッグする時1にしてね
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007
// ここから編集する
int W, H;
vector<string> vv;
string dp[70][70];

bool integer(char c) { return c >= '0' && c <= '9'; }

string parse(string str) {
  int start = 0;
  for (start = 0; str[start] == '0' && start < str.size(); start++) {
  }
  return str.substr(start);
}

bool lt(string left, string right) {
  //    left = parse(left);
  //    right = parse(right);
  if (left.size() < right.size()) {
    return true;
  } else if (left.size() > right.size()) {
    return false;
  } else {
    rep(i, 0, left.size()) {
      if (left[i] != right[i]) {
        return left[i] < right[i];
      }
    }
  }
  return false;
}

string dfs(int sh, int sw) {
  if (dp[sh][sw] != ".")
    return dp[sh][sw];
  if (!integer(vv[sh][sw])) {
    return "";
  }
  if (sh < H - 1 && sw < W - 1) {
    string right;
    right += vv[sh][sw];
    right += dfs(sh, sw + 1);

    string down;
    down += vv[sh][sw];
    down += dfs(sh + 1, sw);
    if (lt(right, down)) {
      return dp[sh][sw] = down;
    } else {
      return dp[sh][sw] = right;
    }
  } else if (sh < H - 1 && sw == W - 1) {
    string down;
    down += vv[sh][sw];
    down += dfs(sh + 1, sw);
    return dp[sh][sw] = down;

  } else if (sh == H - 1 && sw < W - 1) {
    string right;
    right += vv[sh][sw];
    right += dfs(sh, sw + 1);
    return dp[sh][sw] = right;

  } else if (sh == H - 1 && sw == W - 1) {
    string ret = "";
    ret += vv[sh][sw];
    return dp[sh][sw] = ret;
  }
  return "";
}

bool solve() {
  cin >> W >> H;
  if (W == 0 && H == 0) {
    return false;
  }
  vv.resize(H);
  rep(i, 0, 70) {
    rep(j, 0, 70) { dp[i][j] = "."; }
  }

  rep(i, 0, H) { cin >> vv[i]; }

  string ans = "";
  rep(sh, 0, H) {
    rep(sw, 0, W) {
      string tmp = parse(dfs(sh, sw));
      if (lt(ans, tmp)) {
        ans = tmp;
      }
    }
  }
  cout << ans << endl;

  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (solve()) {
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
// #define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c), end(c)
// #define EXIST(s, e) ((s).find(e)!=(s).end())
// #define SORT(c) sort(begin(c),end(c))
// #define pb emplace_back
// #define MP make_pair
// #define SZ(a) int((a).size())

// #define LOCAL 0
// #ifdef LOCAL
// #define DEBUG(s) cout << (s) << endl
// #define dump(x)  cerr << #x << " = " << (x) << endl
// #define BR cout << endl;
// #else
// #define DEBUG(s) do{}while(0)
// #define dump(x) do{}while(0)
// #define BR
// #endif

// 改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 // デバッグする時1にしてね
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007
// ここから編集する
int W, H;
vector<string> vv;
string dp[70][70][70][70];

bool integer(char c) { return c >= '0' && c <= '9'; }

string parse(string str) {
  int start = 0;
  for (start = 0; str[start] == '0' && start < str.size(); start++) {
  }
  return str.substr(start);
}

bool lt(string left, string right) {
  //    left = parse(left);
  //    right = parse(right);
  if (left.size() < right.size()) {
    return true;
  } else if (left.size() > right.size()) {
    return false;
  } else {
    rep(i, 0, left.size()) {
      if (left[i] != right[i]) {
        return left[i] < right[i];
      }
    }
  }
  return false;
}

string dfs(int sh, int sw, int th, int tw) {
  if (dp[sh][sw][th][tw] != ".") {
    return dp[sh][sw][th][tw];
  }
  if (!integer(vv[sh][sw]) || !integer(vv[th][tw])) {
    return dp[sh][sw][th][tw] = "";
  }
  string ret = "";
  if (sh == th && sw == tw) {
    ret += vv[sh][sw];
    return dp[sh][sw][th][tw] = ret;
  }
  if (th - sh + tw - sw == 1) {
    ret += vv[sh][sw];
    ret += vv[th][tw];
    return dp[sh][sw][th][tw] = ret;
  }

  rep(mh, sh, th + 1) {
    rep(mw, sw, tw + 1) {
      if (sh == mh && sw == mw) {
        continue;
      }
      if (mh == th && mw == tw) {
        continue;
      }
      string left = dfs(sh, sw, mh, mw);
      string right = dfs(mh, mw, th, tw);
      if (left.size() > 0 && right.size() > 0) {
        string tmp = "";
        tmp += left.substr(0, left.size() - 1);
        tmp += right;
        if (lt(ret, tmp)) {
          ret = tmp;
        }
      }
    }
  }
  return dp[sh][sw][th][tw] = ret;
}

bool solve() {
  cin >> W >> H;
  if (W == 0 && H == 0) {
    return false;
  }
  vv.resize(H);
  rep(i, 0, 70) {
    rep(j, 0, 70) {
      rep(k, 0, 70) {
        rep(l, 0, 70) { dp[i][j][k][l] = "."; }
      }
    }
  }

  rep(i, 0, H) { cin >> vv[i]; }

  string ans = "";
  rep(sh, 0, H) {
    rep(th, sh, H) {
      rep(sw, 0, W) {
        rep(tw, sw, W) {
          string tmp = parse(dfs(sh, sw, th, tw));
          if (lt(ans, tmp)) {
            ans = tmp;
          }
        }
      }
    }
  }
  cout << ans << endl;

  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (solve()) {
  }

  return 0;
}