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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int w, h;

#define MAX 70
char brd[MAX][MAX];

string memo[MAX][MAX];

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

class num {
public:
  string n;

  num() : n("") {}
  num(string n) : n(f(n)) {}

  string f(string s) {
    if (s[0] == '0' && s.size() > 1)
      return f(s.substr(1));
    return s;
  }

  bool operator<(const num &r) const {
    if (n.size() == r.n.size())
      return n < r.n;
    return n.size() < r.n.size();
  }
};

string dfs(int y, int x) {
  if (memo[y][x] != "")
    return memo[y][x];

  string ans = "";
  rep(d, 2) {
    int ty = y + dy[d], tx = x + dx[d];
    if (!(0 <= ty && ty < h))
      continue;
    if (!(0 <= tx && tx < w))
      continue;
    if (!isdigit(brd[ty][tx]))
      continue;
    string ret = brd[y][x] + dfs(ty, tx);
    if (ans.size() == ret.size())
      ans = max(ans, ret);
    else if (ans.size() < ret.size())
      ans = ret;
  }
  if (ans == "")
    ans = brd[y][x];
  return memo[y][x] = ans;
}

int main(void) {
  while (cin >> w >> h, w | h) {
    rep(j, MAX) rep(i, MAX) memo[j][i] = "";

    rep(j, h) rep(i, w) cin >> brd[j][i];

    num ans = num();
    rep(j, h) rep(i, w) if (isdigit(brd[j][i])) ans = max(ans, num(dfs(j, i)));

    cout << ans.n << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int w, h;

#define MAX 70
char brd[MAX][MAX];

string memo[MAX][MAX];

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

class num {
public:
  string n;

  num() : n("") {}
  num(string n) : n(f(n)) {}

  string f(string s) {
    if (s[0] == '0' && s.size() > 1)
      return s.substr(1);
    return s;
  }

  bool operator<(const num &r) const {
    if (n.size() == r.n.size())
      return n < r.n;
    return n.size() < r.n.size();
  }
};

string dfs(int y, int x) {
  if (memo[y][x] != "")
    return memo[y][x];

  string ans = "@";
  rep(d, 2) {
    int ty = y + dy[d], tx = x + dx[d];
    if (!(0 <= ty && ty < h))
      continue;
    if (!(0 <= tx && tx < w))
      continue;
    if (!isdigit(brd[ty][tx]))
      continue;
    string ret = brd[y][x] + dfs(ty, tx);
    if (ans.size() < ret.size())
      ans = ret;
  }
  if (ans == "@" && brd[y][x] == '0')
    ans = "";
  if (ans == "@")
    ans = brd[y][x];
  return memo[y][x] = ans;
}

int main(void) {
  while (cin >> w >> h, w | h) {
    rep(j, MAX) rep(i, MAX) memo[j][i] = "";

    rep(j, h) rep(i, w) cin >> brd[j][i];

    num ans = num();
    rep(j, h) rep(i, w) if (isdigit(brd[j][i])) ans = max(ans, num(dfs(j, i)));

    cout << ans.n << endl;
  }
  return 0;
}