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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second
#define DBG(X) cout << " " << #X << " : " << X << endl

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;

struct Input {
  int n;
  char c;
  char str[1024];
  string s;
  ll l;
  double d;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  double nextDouble() {
    scanf("%lf", &d);
    return d;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

string amax(const string &s1, const string &s2) {
  if (s1.size() != s2.size())
    return s1.size() > s2.size() ? s1 : s2;
  for (int i = 0; i < s1.size(); i++) {
    if (s1[i] != s2[i])
      return s1[i] > s2[i] ? s1 : s2;
  }
  return s1;
}
bool isNum(char c) { return c >= '0' && c <= '9'; }

int main() {
  while (1) {
    int w = in.nextInt(), h = in.nextInt();
    if (h == 0 && w == 0)
      break;
    vector<string> s(h);
    vector<vector<string>> d(h + 1, vector<string>(w + 1));
    rep(i, h) s[i] = in.next();
    int m = 0;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (isNum(s[i][j]))
          m = max(m, s[i][j] - '0');
      }
    if (m == 0) {
      printf("0\\n");
      continue;
    }
    string ans = "";

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (!isNum(s[i][j]))
          continue;
        if (s[i][j] != '0')
          d[i][j] = s[i][j];
        if (i > 0) {
          if (d[i - 1][j] == "" && s[i][j] == '0')
            ;
          else
            d[i][j] = amax(d[i][j], d[i - 1][j] + s[i][j]);
        }
        if (j > 0) {
          if (d[i][j - 1] == "" && s[i][j] == '0')
            ;
          else
            d[i][j] = amax(d[i][j], d[i][j - 1] + s[i][j]);
        }
        ans = amax(ans, d[i][j]);
      }
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
const double PI = acos(-1);

string s[110];
string ans;
int w, h;
set<pair<pii, string>> st;

void calc(int i, int j, string num) {
  if (st.count(mp(mp(i, j), num)))
    return;
  st.insert(mp(mp(i, j), num));
  if (i >= h || j >= w) {
    if (ans.size() < num.size())
      ans = num;
    else if (ans.size() > num.size())
      return;
    else {
      for (int i = 0; i < ans.size(); i++) {
        if (ans[i] > num[i])
          return;
      }
      ans = num;
    }
    return;
  }
  if (s[i][j] >= '0' && s[i][j] <= '9') {
    num += s[i][j];
    calc(i + 1, j, num);
    calc(i, j + 1, num);
  } else
    calc(INF, INF, num);
}

int main() {
  while (cin >> w >> h && w && h) {
    st.clear();
    rep(i, h) cin >> s[i];
    ans = "";
    rep(i, h) rep(j, w) calc(i, j, "");
    cout << ans << endl;
  }
}