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

#include "bits/stdc++.h"
#include <fstream>
#include <sys/timeb.h>

using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) replrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(a) a.begin(), a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS 1e-9
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<double>> vvd;
typedef vector<vector<P>> vvP;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll;
typedef priority_queue<P, vector<P>, greater<P>> pqlP;
typedef pair<int, pair<int, int>> Edge;
typedef vector<Edge> vE;
typedef priority_queue<Edge, vector<Edge>, greater<Edge>> pqlE;

bool isNum(char c) { return (c >= '0' && c <= '9'); }
bool isPlusNum(char c) { return (c >= '1' && c <= '9'); }

string maxnum(string a, string b) {
  if (a.length() > b.length()) {
    return a;
  } else if (a.length() < b.length()) {
    return b;
  } else {
    return max(a, b);
  }
}

int main() {
  while (true) {
    vector<pair<int, string>> nums;
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    vs f(H);
    rep(i, H) { cin >> f[i]; }
    vvs dp(H, vs(W, ""));
    reprev(i, H) {
      reprev(j, W) {
        if (isNum(f[i][j])) {
          if (i == H - 1) {
            if (j == W - 1) {
              dp[i][j] += f[i][j];
            } else {
              dp[i][j] += f[i][j] + dp[i][j + 1];
            }
          } else if (j == W - 1) {
            dp[i][j] += f[i][j] + dp[i + 1][j];
          } else {
            dp[i][j] += f[i][j] + maxnum(dp[i + 1][j], dp[i][j + 1]);
          }
        }
      }
    }
    string ans;
    rep(i, H) rep(j, W) {
      if (dp[i][j].length() == 0 || dp[i][j][0] == '0')
        continue;
      ans = maxnum(ans, dp[i][j]);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
#include <fstream>
#include <sys/timeb.h>

using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) replrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(a) a.begin(), a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS 1e-9
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<double>> vvd;
typedef vector<vector<P>> vvP;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll;
typedef priority_queue<P, vector<P>, greater<P>> pqlP;
typedef pair<int, pair<int, int>> Edge;
typedef vector<Edge> vE;
typedef priority_queue<Edge, vector<Edge>, greater<Edge>> pqlE;

bool isNum(char c) { return (c >= '0' && c <= '9'); }
bool isPlusNum(char c) { return (c >= '1' && c <= '9'); }

int main() {
  while (true) {
    vector<pair<int, string>> nums;
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    vs f(H);
    rep(i, H) { cin >> f[i]; }
    stack<tuple<int, int, vc>> st;
    rep(i, H) {
      rep(j, W) {
        if (isPlusNum(f[i][j]) && (i == 0 || !isPlusNum(f[i - 1][j])) &&
            (j == 0 || !isPlusNum(f[i][j - 1]))) {
          st.push(mt(i, j, vc(1, f[i][j])));
          cout << f[i][j] << endl;
        }
      }
    }
    while (!st.empty()) {
      int y = get<0>(st.top());
      int x = get<1>(st.top());
      vc s = get<2>(st.top());
      st.pop();
      if (y != H - 1 && isNum(f[y + 1][x])) {
        s.push_back(f[y + 1][x]);
        st.push(mt(y + 1, x, s));
        s.resize(s.size() - 1);
      }
      if (x != W - 1 && isNum(f[y][x + 1])) {
        s.push_back(f[y][x + 1]);
        st.push(mt(y, x + 1, s));
      } else {
        nums.push_back(mp((int)s.size(), string(all(s))));
      }
    }

    sort(all(nums));
    if (nums.size() == 0) {
      cout << 0 << endl;
    } else {
      cout << nums[nums.size() - 1].second << endl;
    }
  }

  return 0;
}