Problem p00707 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

template <typename T> T &chmin(T a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T a, const T &b) { return a = max(a, b); }

using ll = long long;
using ld = long double;

int W, H;
string mass[75][75];
string res[70][70];

bool f(const string &a, const string &b) {
  if (a.size() != b.size())
    return a.size() > b.size();
  REP(i, a.size()) if (a[i] != b[i]) return a[i] > b[i];
  return false;
}

string dfs(int h, int w) {
  if (res[h][w] != "")
    return res[h][w];
  if (mass[h][w] == "*")
    return res[h][w] = "";
  if (mass[h + 1][w] == "*" and mass[h][w + 1] == "*")
    return res[h][w] = mass[h][w];
  if (mass[h + 1][w] == "*")
    return res[h][w] = mass[h][w].append(dfs(h, w + 1));
  if (mass[h][w + 1] == "*")
    return res[h][w] = mass[h][w].append(dfs(h + 1, w));
  string str1 = dfs(h + 1, w);
  string str2 = dfs(h, w + 1);
  if (f(str1, str2))
    return res[h][w] = mass[h][w].append(str1);
  return res[h][w] = mass[h][w].append(str2);
}

int main() {
  while (cin >> W >> H and W + H) {
    REP(i, 75) REP(j, 75) mass[i][j] = "*";
    REP(i, 70) REP(j, 70) res[i][j] = "";
    REP(i, H) {
      string tmp;
      cin >> tmp;
      REP(j, W)
      mass[i][j] = ('0' <= tmp[j] and tmp[j] <= '9' ? string(&tmp[j], 1) : "*");
    }
    vector<string> strs;
    REP(i, H) REP(j, W) dfs(i, j);

    REP(i, 70) REP(j, 70) {
      int fr = 0;
      while (fr < res[i][j].size() and res[i][j][fr] == '0')
        fr++;
      if (fr == res[i][j].size())
        continue;
      strs.push_back(res[i][j].substr(fr, res[i][j].size() - fr));
    }
    sort(ALL(strs), f);
    cout << strs[0] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

using ll = long long;
using ld = long double;

const int INF = 1e9;
const ld eps = 1e-9, pi = acos(-1.0);

int W, H;
string mass[75][75];
string res[70][70];

string dfs(int h, int w, string str) {
  if (res[h][w] != "")
    return res[h][w];
  if (mass[h + 1][w] == "*" and mass[h][w + 1] == "*" and mass[h][w] != "*")
    return res[h][w] = str.append(mass[h][w]);
  str.append(mass[h][w]);
  //	cout << str << endl;
  if (mass[h + 1][w] == "*")
    return res[h][w] = dfs(h, w + 1, str);
  if (mass[h][w + 1] == "*")
    return res[h][w] = dfs(h + 1, w, str);
  return res[h][w] = max(dfs(h, w + 1, str), dfs(h + 1, w, str));
}

bool f(const string &a, const string &b) {
  if (a.size() < b.size())
    return false;
  if (a.size() > b.size())
    return true;
  REP(i, a.size()) if (a[i] < b[i]) return false;
  return true;
}

int main() {
  while (cin >> W >> H and W + H) {
    REP(i, 75) REP(j, 75) mass[i][j] = "*";
    REP(i, 70) REP(j, 70) res[i][j] = "";
    REP(i, H) {
      string tmp;
      cin >> tmp;
      REP(j, W)
      mass[i][j] =
          (0 <= tmp[j] - '0' and tmp[j] - '0' <= 9 ? tmp.substr(j, 1) : "*");
    }
    REP(i, H) REP(j, W) if (mass[i][j] != "*") dfs(i, j, "");
    vector<string> vals;
    REP(i, H) REP(j, W) if (res[i][j] != "") {
      int fr = 0;
      while (res[i][j][fr] == '0' and fr < res[i][j].size())
        fr++;
      if (fr == res[i][j].size())
        continue;
      vals.push_back(res[i][j].substr(fr, res[i][j].size() - fr));
    }
    // REP(i, vals.size()) cout << vals[i] << endl;
    // cout << "==" << endl;
    sort(ALL(vals), f);
    // vals.erase(unique(ALL(vals)), vals.end());
    vector<string> res;
    REP(i, vals.size()) if (vals[i] != "") res.push_back(vals[i]);
    // cout << "size : " << vals.size() << endl;
    cout << res[0] << endl;
    // cout << "-------------" << endl;
  }

  return 0;
}