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

// #include "bits/stdc++.h"

#include <array>
#include <iostream>
// #include <map>
#include <string>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

int w; // j
int h; // i
string max_s(70, ' ');
array<array<char, 70>, 70> field;  // [i][j]
array<array<string, 70>, 70> memo; // [i][j]

inline string big(const string &a, const string &b) {
  if (a.size() == b.size()) {
    if (a > b) {
      return a;
    } else {
      return b;
    }
  }
  if (a.size() > b.size()) {
    return a;
  } else {
    return b;
  }
}

string _trace(int i, int j) {
  if (memo[i][j] != "") {
    return memo[i][j];
  }
  int new_i = i + 1;
  int new_j = j + 1;
  string res1 = "";
  string res2 = "";
  if (new_i < h and isdigit(field[new_i][j])) {
    res1 = _trace(new_i, j);
  }
  if (new_j < w and isdigit(field[i][new_j])) {
    res2 = _trace(i, new_j);
  }
  memo[i][j] = field[i][j] + big(res1, res2);
  return memo[i][j];
}

inline bool myIsdigit(char c) { return ('1' <= c and c <= '9'); }

string trace(int i, int j) {
  if (myIsdigit(field[i][j]) == false) {
    return "";
  }
  if (i > 0 and myIsdigit(field[i - 1][j])) {
    return "";
  }
  if (j > 0 and myIsdigit(field[i][j - 1])) {
    return "";
  }
  auto res = _trace(i, j);
  return res;
}

void solve() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> field[i][j];
      memo[i][j] = "";
    }
  }
  max_s = "";
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      max_s = big(max_s, trace(i, j));
    }
  }
  cout << max_s << endl;
}

int main() {

  while (true) {
    cin >> w >> h;
    if (w == 0 and h == 0) {
      break;
    }
    solve();
  }

  // std::cout << "\\e[38;5;0m\\e[48;5;40m --- end ---  \\e[m" << std::endl; //
  // debug
  return 0;
}

Error: TLE

Fixed submission:

// #include "bits/stdc++.h"

#include <array>
#include <iostream>
// #include <map>
#include <string>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

int w; // j
int h; // i
string max_s(72, ' ');
array<array<char, 72>, 72> field; // [i][j]
array<char, 72> num;
string str(72, ' ');

string big(const string &a, const string &b) {
  if (a.size() == b.size()) {
    if (a > b) {
      return a;
    } else {
      return b;
    }
  }
  if (a.size() > b.size()) {
    return a;
  } else {
    return b;
  }
}

void check(int d) {
  int padding = 0;
  for (int k = 0; k < d; k++) {
    if (num[k] == '0') {
      padding++;
    } else {
      break;
    }
  }
  int s = d - padding; // as size
  str.resize(s);
  for (int k = 0; k < s; k++) {
    str[k] = num[k + padding];
  }
  max_s = big(max_s, str);
}

void _trace(int i, int j, int d /* depth */) {
  num[d] = field[i][j];
  d++;
  int new_i = i + 1;
  int new_j = j + 1;
  bool flg_end = true;
  if (new_i < h and isdigit(field[new_i][j])) {
    _trace(new_i, j, d);
    flg_end = false;
  }
  if (new_j < w and isdigit(field[i][new_j])) {
    _trace(i, new_j, d);
    flg_end = false;
  }
  if (flg_end) {
    check(d);
  }
}

void trace(int i, int j) {
  if (isdigit(field[i][j]) == false) {
    return;
  }
  if (i > 0 and isdigit(field[i - 1][j])) {
    return;
  }
  if (j > 0 and isdigit(field[i][j - 1])) {
    return;
  }
  _trace(i, j, 0);
}

void solve() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> field[i][j];
    }
  }
  max_s = "";
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      trace(i, j);
    }
  }
  cout << max_s << endl;
}

int main() {

  while (true) {
    cin >> w >> h;
    if (w == 0 and h == 0) {
      break;
    }
    solve();
  }

  // std::cout << "\\e[38;5;0m\\e[48;5;40m --- end ---  \\e[m" << std::endl; //
  // debug
  return 0;
}