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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int w, h;
string s[77];

string bigger(string s1, string s2) {
  if (s1.length() > s2.length())
    return s1;
  else if (s1.length() < s2.length())
    return s2;
  else if (s1 > s2)
    return s1;
  else
    return s2;
}

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> w >> h;
    if (w == 0)
      break;
    rep(i, h) cin >> s[i];
    string res = "";
    string dp[77][77];
    rep(i, h) rep(j, w) {
      string ss = bigger(dp[i + 1][j], dp[i][j + 1]);
      if (!isdigit(s[i][j]) || (s[i][j] == '0' && ss == ""))
        continue;
      dp[i + 1][j + 1] = ss + s[i][j];
      res = bigger(res, ss + s[i][j]);
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int w, h;
string s[77];

int d[4] = {0, 1, 0};
bool vis[77][77];

bool is_big(string s1, string s2) {
  if (s1.length() == s2.length()) {
    int i = 0;
    while (i < s1.length() && s1[i] == s2[i])
      i++;
    return i == s1.length() || s1[i] >= s2[i];
  } else
    return s1.length() > s2.length();
}

string dfs(int i, int j) {
  vis[i][j] = true;
  string res = "";
  rep(dd, 2) {
    int ni = i + d[dd], nj = j + d[dd + 1];
    if (ni < 0 || ni >= h || nj < 0 || nj >= w || !isdigit(s[ni][nj]) ||
        vis[ni][nj])
      continue;
    string a = dfs(ni, nj);
    if (is_big(a, res))
      res = a;
  }
  vis[i][j] = false;
  return s[i][j] + res;
}

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> w >> h;
    if (w == 0)
      break;
    rep(i, h) cin >> s[i];
    string res = "";
    memset(vis, 0, sizeof(vis));
    rep(i, h) rep(j, w) {
      if (!isdigit(s[i][j]))
        continue;
      if ((i > 0 && isdigit(s[i - 1][j])) || (j > 0 && isdigit(s[i][j - 1])))
        continue;
      string a = dfs(i, j);
      if (is_big(a, res))
        res = a;
    }
    cout << res << endl;
  }
  return 0;
}