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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define clear(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

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

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

string bigger(const string &a, const string &b) {
  if (a.size() > b.size() || a.size() == b.size() && a > b)
    return a;
  else
    return b;
}
int main() {
  int w, h;
  char c[128][128];
  while (cin >> w >> h, w) {
    for (int i = 0; i < h; ++i)
      scanf("%s", c[i]);

    string res = "";
    string dp[128][128];
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        if (isdigit(c[y][x])) {
          string t = bigger(dp[y][x + 1], dp[y + 1][x]) + c[y][x];
          if (t != "0") {
            dp[y + 1][x + 1] = t;
            res = bigger(res, t);
          }
        }
      }
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define clear(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

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

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

string bigger(const string &a, const string &b) {
  if (a.size() > b.size() || a.size() == b.size() && a > b)
    return a;
  else
    return b;
}
int w, h;
char c[128][128];
string dfs(int x, int y) {
  if (x < 0 || x >= w || y < 0 || y >= h || !isdigit(c[y][x]))
    return "";
  else
    return c[y][x] + bigger(dfs(x + 1, y), dfs(x, y + 1));
}
int main() {
  while (cin >> w >> h, w) {
    for (int i = 0; i < h; ++i)
      scanf("%s", c[i]);
    string res = "";
    for (int y = 0; y < h; ++y)
      for (int x = 0; x < w; ++x)
        if (isdigit(c[y][x]) && c[y][x] != '0')
          res = bigger(res, dfs(x, y));
    cout << res << endl;
  }
}