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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

const double EPS = 1e-9;

string maxs(string a, string b) {
  while (a.size() >= 1) {
    if (a[0] != '0')
      break;
    a = a.substr(1, a.size() - 1);
  }
  while (b.size() >= 1) {
    if (b[0] != '0')
      break;
    b = b.substr(1, b.size() - 1);
  }
  if (a.length() > b.length())
    return a;
  if (a.length() < b.length())
    return b;
  for (int i = 0; i < a.length(); i++) {
    if (a[i] > b[i])
      return a;
    if (a[i] < b[i])
      return b;
  }
  return a;
}

string choos(string a, string b) {
  if (a.length() > b.length())
    return a;
  else if (a.length() < b.length())
    return b;
  else {
    for (int i = 0; i < a.length(); i++) {
      if (a[i] > b[i])
        return a;
      else if (a[i] < b[i])
        return b;
    }
    return a;
  }
}
int dx[] = {0, -1};
int dy[] = {-1, 0};
int main() {
  while (true) {
    int W, H;
    cin >> W >> H;
    if (W == 0)
      break;
    vector<vector<char>> C(H + 2, vector<char>(W + 2, 'A'));
    vector<vector<string>> memo(H + 2, vector<string>(W + 2));
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> C[i][j];
      }
    }
    string ret = "0";
    for (int y = H + 1; y >= 0; y--) {
      for (int x = W + 1; x >= 0; x--) {
        if (memo[y][x] == "" && isdigit(C[y][x])) {
          memo[y][x] = C[y][x];
          ret = maxs(ret, string(1, C[y][x]));
        }
        if (isdigit(C[y][x])) {
          for (int i = 0; i < 2; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (isdigit(C[ny][nx])) {
              memo[ny][nx] = choos(C[ny][nx] + memo[y][x], memo[ny][nx]);
              ret = maxs(ret, C[ny][nx] + memo[y][x]);
            }
          }
        }
      }
    }
    // for(int i=0;i<H+2;i++){
    //     for(int j=0;j<W+2;j++){
    //         cerr << memo[i][j] << " ";
    //     }
    //     cerr << endl;
    // }
    cout << ret << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

const double EPS = 1e-9;

struct State {
  string str;
  int y;
  int x;
  State(string str, int y, int x) : str(str), y(y), x(x){};
};

string maxs(string a, string b) {
  while (a.size() >= 1) {
    if (a[0] != '0')
      break;
    a = a.substr(1, a.size() - 1);
  }
  while (b.size() >= 1) {
    if (b[0] != '0')
      break;
    b = b.substr(1, b.size() - 1);
  }
  if (a.length() > b.length())
    return a;
  if (a.length() < b.length())
    return b;
  for (int i = 0; i < a.length(); i++) {
    if (a[i] > b[i])
      return a;
    if (a[i] < b[i])
      return b;
  }
  return a;
}

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;
    if (W == 0)
      break;
    vector<vector<char>> C(H + 2, vector<char>(W + 2, 'A'));
    vector<vector<string>> memo(H + 2, vector<string>(W + 2));
    stack<State> sta;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> C[i][j];
        if (isalpha(C[i][j]))
          continue;
        string s;
        s = C[i][j];
        sta.push(State(s, i, j));
      }
    }
    string ret = "0";
    vector<vector<char>> alr;
    while (!sta.empty()) {
      string str = sta.top().str;
      ret = maxs(ret, str);
      int y = sta.top().y;
      int x = sta.top().x;
      sta.pop();

      const int dx[] = {1, 0};
      const int dy[] = {0, 1};
      for (int i = 0; i < 2; i++) {
        int ny = y + dy[i], nx = x + dx[i];
        if (isalpha(C[ny][nx]))
          continue;
        string newstr = str + C[ny][nx];
        sta.push(State(newstr, ny, nx));
      }
    }
    cout << ret << endl;
  }

  return 0;
}