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

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF ((int)(1 << 30))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(x) cout << #x << " :" << x << endl;
#define EPS 1e-12

template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, -1, 1, 0}, dy[4] = {1, 0, 0, -1};

int clamp(int a, int mi, int ma) { return max(mi, min(a, ma)); }
bool isnum(char a) { return ('0' <= a && a <= '9'); }
string big(string a, string b) {
  if (a.size() > b.size()) {
    return a;
  }
  if (a.size() < b.size()) {
    return b;
  }
  return max(a, b);
}

char a[100][100];
string flag[100][100];
string ans;
void hoge(string &s, int y, int x) {
  flag[y][x] = big(s + flag[y][x + 1], s + flag[y + 1][x]);
  if (s != "0")
    ans = big(ans, flag[y][x]);
  return;
}

int main() {
  int w, h;
  while (cin >> w >> h && w) {

    REP(i, 100) REP(j, 100) {
      a[i][j] = 'a';
      flag[i][j] = "";
    }
    ans = "";
    REP(i, h) {
      REP(j, w) { cin >> a[i][j]; }
    }

    for (int y = h - 1; y >= 0; y--) {
      for (int x = w - 1; x >= 0; x--) {
        if (isnum(a[y][x])) {
          string str;
          str += a[y][x];
          hoge(str, y, x);
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF ((int)(1 << 30))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(x) cout << #x << " :" << x << endl;
#define EPS 1e-12

template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, -1, 1, 0}, dy[4] = {1, 0, 0, -1};

int clamp(int a, int mi, int ma) { return max(mi, min(a, ma)); }
bool isnum(char a) { return ('0' <= a && a <= '9'); }
string big(string a, string b) {
  if (a.size() > b.size()) {
    return a;
  }
  if (a.size() < b.size()) {
    return b;
  }
  return max(a, b);
}

char a[100][100];
string flag[100][100];
string ans;
void hoge(string &s, int y, int x) {
  if (s < flag[y][x])
    return;
  ans = big(ans, s);
  flag[y][x] = s;
  int DY[2] = {0, 1}, DX[2] = {1, 0};
  REP(i, 2) {
    int Y = y + DY[i];
    int X = x + DX[i];
    if (isnum(a[Y][X])) {
      s += a[Y][X];
      hoge(s, Y, X);
      s.pop_back();
    }
  }

  return;
}

int main() {
  int w, h;
  while (cin >> w >> h && w) {

    REP(i, 100) REP(j, 100) {
      a[i][j] = 'a';
      flag[i][j] = "";
    }
    ans = "";
    REP(i, h) {
      REP(j, w) { cin >> a[i][j]; }
    }

    REP(y, h) {
      REP(x, w) {
        if (isnum(a[y][x]) && flag[y][x] == "" && a[y][x] != '0') {
          string str;
          str += a[y][x];
          hoge(str, y, x);
        }
      }
    }
    cout << ans << endl;
  }
}