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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

string strMax(string str1, string str2) {
  if (str1.size() != str2.size())
    return str1.size() > str2.size() ? str1 : str2;
  string res = str1;
  for (int i = 0; i < str1.size(); i++) {
    if (str1[i] - '0' == str2[i] - '0')
      continue;
    res = str1[i] - '0' > str2[i] - '0' ? str1 : str2;
    break;
  }
  return res;
}

bool comp(const string &a, const string &b) {
  if (a.size() != b.size())
    return a.size() > b.size();
  for (int i = 0; i < a.size(); i++) {
    if (a[i] - '0' == b[i] - '0')
      continue;
    return a[i] - '0' > b[i] - '0';
  }
}

int main() {
  while (1) {
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    string C[H];
    for (int i = 0; i < H; i++)
      cin >> C[i];

    string dp[H + 1][W + 1];
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        dp[i][j] = C[i][j];

    for (int i = 1; i < W; i++)
      if (isdigit(C[0][i - 1]) && isdigit(C[0][i])) {
        dp[0][i] = dp[0][i - 1] + dp[0][i];
        if (dp[0][i][0] == '0')
          dp[0][i] = dp[0][i].substr(1);
      }

    for (int i = 1; i < H; i++)
      if (isdigit(C[i - 1][0]) && isdigit(C[i][0])) {
        dp[i][0] = dp[i - 1][0] + dp[i][0];
        if (dp[i][0][0] == '0')
          dp[i][0] = dp[i][0].substr(1);
      }

    for (int i = 1; i < H; i++) {
      for (int j = 1; j < W; j++) {
        if ((!isdigit(C[i - 1][j]) && !isdigit(C[i][j - 1])) ||
            !isdigit(C[i][j]))
          continue;
        if (!isdigit(C[i - 1][j]) && isdigit(C[i][j - 1]))
          dp[i][j] = dp[i][j - 1] + dp[i][j];
        else if (isdigit(C[i - 1][j]) && !isdigit(C[i][j - 1]))
          dp[i][j] = dp[i - 1][j] + dp[i][j];
        else
          dp[i][j] = strMax(dp[i - 1][j] + dp[i][j], dp[i][j - 1] + dp[i][j]);
        if (dp[i][j][0] == '0')
          dp[i][j] = dp[i][j].substr(1);
      }
    }

    /*
    cout << "-----------------------------" << endl;
    for(int i = 0; i < H; i++){
      for(int j = 0; j < W; j++){
        cout << dp[i][j] << "  ";
      }
      cout << endl;
    }
    cout << "-----------------------------" << endl;
    */

    vector<string> v;

    if (isdigit(C[H - 1][W - 1]))
      v.push_back(dp[H - 1][W - 1]);

    for (int i = 0; i < H - 1; i++)
      if (!isdigit(C[i + 1][W - 1]) && isdigit(C[i][W - 1]))
        v.push_back(dp[i][W - 1]);

    for (int i = 0; i < W - 1; i++)
      if (!isdigit(C[H - 1][i + 1]) && isdigit(C[H - 1][i]))
        v.push_back(dp[H - 1][i]);

    for (int i = 0; i < H - 1; i++)
      for (int j = 0; j < W - 1; j++)
        if (!isdigit(C[i + 1][j]) && !isdigit(C[i][j + 1]) && isdigit(C[i][j]))
          v.push_back(dp[i][j]);

    sort(v.begin(), v.end(), comp);
    cout << v[0] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

string strMax(string str1, string str2) {
  if (str1.size() != str2.size())
    return str1.size() > str2.size() ? str1 : str2;
  string res = str1;
  for (int i = 0; i < str1.size(); i++) {
    if (str1[i] - '0' == str2[i] - '0')
      continue;
    res = str1[i] - '0' > str2[i] - '0' ? str1 : str2;
    break;
  }
  return res;
}

bool comp(const string &a, const string &b) {
  if (a.size() != b.size())
    return a.size() > b.size();
  for (int i = 0; i < a.size(); i++) {
    if (a[i] - '0' == b[i] - '0')
      continue;
    return a[i] - '0' > b[i] - '0';
  }
}

int main() {
  while (1) {
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    string C[H];
    for (int i = 0; i < H; i++)
      cin >> C[i];

    string dp[H + 1][W + 1];
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        dp[i][j] = C[i][j];

    for (int i = 1; i < W; i++)
      if (C[0][i - 1] != '0' && isdigit(C[0][i - 1]) && isdigit(C[0][i]))
        dp[0][i] = dp[0][i - 1] + dp[0][i];

    for (int i = 1; i < H; i++)
      if (C[i - 1][0] != '0' && isdigit(C[i - 1][0]) && isdigit(C[i][0]))
        dp[i][0] = dp[i - 1][0] + dp[i][0];

    for (int i = 1; i < H; i++) {
      for (int j = 1; j < W; j++) {
        if ((!isdigit(C[i - 1][j]) && !isdigit(C[i][j - 1])) ||
            !isdigit(C[i][j]))
          continue;
        if (!isdigit(C[i - 1][j]) && isdigit(C[i][j - 1]))
          dp[i][j] = dp[i][j - 1] + dp[i][j];
        else if (isdigit(C[i - 1][j]) && !isdigit(C[i][j - 1]))
          dp[i][j] = dp[i - 1][j] + dp[i][j];
        else
          dp[i][j] = strMax(dp[i - 1][j] + dp[i][j], dp[i][j - 1] + dp[i][j]);
        if (dp[i][j][0] == '0')
          dp[i][j] = dp[i][j].substr(1);
      }
    }

    vector<string> v;

    if (isdigit(C[H - 1][W - 1]))
      v.push_back(dp[H - 1][W - 1]);

    for (int i = 0; i < H - 1; i++)
      if (!isdigit(C[i + 1][W - 1]) && isdigit(C[i][W - 1]))
        v.push_back(dp[i][W - 1]);

    for (int i = 0; i < W - 1; i++)
      if (!isdigit(C[H - 1][i + 1]) && isdigit(C[H - 1][i]))
        v.push_back(dp[H - 1][i]);

    for (int i = 0; i < H - 1; i++)
      for (int j = 0; j < W - 1; j++)
        if (!isdigit(C[i + 1][j]) && !isdigit(C[i][j + 1]) && isdigit(C[i][j]))
          v.push_back(dp[i][j]);

    sort(v.begin(), v.end(), comp);
    cout << v[0] << endl;
  }
}