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

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
using namespace std;
typedef pair<string, pair<int, int>> P;

int w, h;
char c[80][80];

int dy[2] = {1, 0};
int dx[2] = {0, 1};
bool f[70][70];

bool is_digit(char c) { return ('0' <= c && c <= '9'); }

bool contain(int y, int x) { return (0 <= y && y < h && 0 <= x && x < w); }

vector<string> res;

void dfs(int y, int x, string s) {
  // cout << y << " " << x << " " <<s << endl;
  if (!contain(y, x) || !is_digit(c[y][x])) {
    res.push_back(s);
    return;
  }
  f[y][x] = true;
  rep(i, 0, 2) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    dfs(ny, nx, s + c[y][x]);
  }
  return;
}

// s1 < s2?
bool comp(string s1, string s2) {
  if (s1.size() == s2.size())
    return s1 < s2;
  return s1.size() < s2.size();
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w + h == 0)
      break;
    rep(i, 0, h) {
      rep(j, 0, w) { cin >> c[i][j]; }
    }
    string d[70][70];
    rep(i, 0, h) {
      rep(j, 0, w) {
        d[i][j] = "";
        if (is_digit(c[i][j]) && c[i][j] != '0') {
          d[i][j] += c[i][j];
        }
      }
    }
    rep(i, 0, h) {
      rep(j, 0, w) {
        string t = "", t1 = "";
        if (i != 0) {
          if (is_digit(c[i][j]) && d[i - 1][j] != "") {
            t = d[i - 1][j] + c[i][j];
          }
        }
        if (j != 0) {
          if (is_digit(c[i][j]) && d[i][j - 1] != "") {
            t1 = d[i][j - 1] + c[i][j];
          }
        }
        // cout << d[i][j] << " ";
        if (comp(d[i][j], t))
          d[i][j] = t;
        if (comp(d[i][j], t1))
          d[i][j] = t1;
        // cout << i << " " << j << " " << d[i][j] << endl;
      }
    }
    string ans = "";
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (comp(ans, d[i][j]))
          ans = d[i][j];
      }
    }
    cout << ans << endl;
    // res.clear();
    // rep(i, 0, h) rep(j, 0, w) f[i][j] = false;
    // rep(i, 0, h){
    //     rep(j, 0, w){
    //         if(f[i][j] == false && is_digit(c[i][j]) && c[i][j] != '0'){
    //             string tmp = "";
    //             dfs(i, j, tmp);
    //         }
    //     }
    // }
    // // rep(i, 0, res.size()){
    // //     cout << res[i] << endl;
    // // }
    // int sz = 0;
    // rep(i, 0, res.size()){
    //     sz = max(sz, (int)res[i].size());
    // }
    // string ans = "";
    // rep(i, 0, res.size()){
    //     if(res[i].size() == sz && ans < res[i]) ans = res[i];
    // }
    // cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
using namespace std;
typedef pair<string, pair<int, int>> P;

int w, h;
char c[80][80];

int dy[2] = {1, 0};
int dx[2] = {0, 1};
bool f[70][70];

bool is_digit(char c) { return ('0' <= c && c <= '9'); }

bool contain(int y, int x) { return (0 <= y && y < h && 0 <= x && x < w); }

vector<string> res;

void dfs(int y, int x, string s) {
  // cout << y << " " << x << " " <<s << endl;
  if (!contain(y, x) || !is_digit(c[y][x])) {
    res.push_back(s);
    return;
  }
  f[y][x] = true;
  rep(i, 0, 2) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    dfs(ny, nx, s + c[y][x]);
  }
  return;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w + h == 0)
      break;
    rep(i, 0, h) {
      rep(j, 0, w) { cin >> c[i][j]; }
    }
    res.clear();
    rep(i, 0, h) rep(j, 0, w) f[i][j] = false;
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (f[i][j] == false && is_digit(c[i][j]) && c[i][j] != '0') {
          string tmp = "";
          dfs(i, j, tmp);
        }
      }
    }
    // rep(i, 0, res.size()){
    //     cout << res[i] << endl;
    // }
    int sz = 0;
    rep(i, 0, res.size()) { sz = max(sz, (int)res[i].size()); }
    string ans = "";
    rep(i, 0, res.size()) {
      if (res[i].size() == sz && ans < res[i])
        ans = res[i];
    }
    cout << ans << endl;
  }
}