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

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

constexpr int INF = 1000000000; /* 1e+9a */
constexpr int MODULO = 1000000007;

typedef int64_t ll;
auto dcomp = [](const string &a, const string &b) {
  if (a.size() != b.size())
    return a.size() < b.size();
  return a < b;
};

string maximize(int i, int j, vector<vector<string>> &dp,
                const vector<vector<char>> &c) {
  if (i >= dp.size())
    return "";
  if (j >= dp[0].size())
    return "";
  if (c[i][j] == '!')
    return "";

  if (dp[i][j].empty()) {
    const string l = maximize(i + 1, j, dp, c);
    const string r = maximize(i, j + 1, dp, c);

    return dp[i][j] = c[i][j] + (dcomp(l, r) ? r : l);
  } else
    return dp[i][j];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int w, h;
  while (cin >> w >> h) {
    if (w == 0 && h == 0)
      break;
    vector<vector<char>> c;
    for (int i = 0; i < h; ++i) {
      c.push_back({});
      for (int j = 0; j < w; ++j) {
        char tmp;
        std::cin >> tmp;
        if ('A' <= tmp && tmp <= 'Z') {
          tmp = '!';
        }
        c[i].push_back(tmp);
      }
    }
    vector<vector<string>> dp(h, vector<string>(w));
    string maxim = "";
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (c[i][j] != '!' && c[i][j] != '0' && dp[i][j].empty()) {
          string st = maximize(i, j, dp, c);
          maxim = max(maxim, st, dcomp);
        }
      }
    }
    cout << maxim << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

constexpr int INF = 1000000000; /* 1e+9a */
constexpr int MODULO = 1000000007;

typedef int64_t ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int w, h;
  while (cin >> w >> h) {
    if (w == 0 && h == 0)
      break;
    vector<vector<char>> c;
    for (int i = 0; i < h; ++i) {
      c.push_back({});
      for (int j = 0; j < w; ++j) {
        char tmp;
        std::cin >> tmp;
        if ('A' <= tmp && tmp <= 'Z') {
          tmp = '!';
        }
        c[i].push_back(tmp);
      }
    }
    vector<vector<bool>> is_subseq(h, vector<bool>(w));
    string maxim = "";
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (c[i][j] != '!' && c[i][j] != '0' && !is_subseq[i][j]) {
          stack<pair<int, int>> Nxy;
          stack<string> Nst;
          Nxy.push({i, j});
          Nst.push(string("") + c[i][j]);
          while (!Nxy.empty()) {
            auto xy = Nxy.top();
            auto st = Nst.top();
            Nxy.pop(), Nst.pop();
            int x = xy.first;
            int y = xy.second;
            bool pushed = false;
            if (x + 1 < h) {
              if (c[x + 1][y] != '!') {
                is_subseq[x + 1][y] = true;
                Nxy.push({x + 1, y});
                Nst.push(st + c[x + 1][y]);
                pushed = true;
              }
            }
            if (y + 1 < w) {
              if (c[x][y + 1] != '!') {
                is_subseq[x][y + 1] = true;
                Nxy.push({x, y + 1});
                Nst.push(st + c[x][y + 1]);
                pushed = true;
              }
            }
            if (!pushed) {
              maxim = max(st, maxim, [](const string &a, const string &b) {
                if (a.size() == b.size())
                  return a < b;
                return a.size() < b.size();
              });
            }
          }
        }
      }
    }
    cout << maxim << '\\n';
  }
}