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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int w, h;
char c[71][71];
string result[71][71];

bool compare(string s1, string s2) {
  int l1 = s1.length(), l2 = s2.length();
  if (l1 > l2)
    return true;
  else if (l1 < l2)
    return false;
  else {
    for (int i = 0; i < l1; i++) {
      if (s1[i] > s2[i])
        return true;
      else if (s1[i] < s2[i])
        return false;
    }
  }
  return true;
}

bool ismax(string s1, string s2) {
  while (s1 != "" && s1[0] == '0') {
    s1 = s1.substr(1);
  }
  while (s2 != "" && s2[0] == '0') {
    s2 = s2.substr(1);
  }
  if (s1 == "")
    s1 = "0";
  if (s2 == "")
    s2 = "0";
  int l1 = s1.length(), l2 = s2.length();
  if (l1 > l2)
    return true;
  else if (l1 < l2)
    return false;
  else {
    for (int i = 0; i < l1; i++) {
      if (s1[i] > s2[i])
        return true;
      else if (s1[i] < s2[i])
        return false;
    }
  }
  return true;
}

string solve(int x, int y) {
  if (result[x][y] != "")
    return result[x][y];
  // cout << now << endl;
  string now = "", now2 = "";
  now += c[x][y];
  now2 += c[x][y];
  if (x + 1 < h && isdigit(c[x + 1][y])) {
    now += solve(x + 1, y);
  }
  if (y + 1 < w && isdigit(c[x][y + 1])) {
    now2 += solve(x, y + 1);
  }
  if (compare(now, now2))
    result[x][y] = now;
  else
    result[x][y] = now2;
  return result[x][y];
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> c[i][j];
        result[i][j] = "";
      }
    }
    string ans = "0";
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (isdigit(c[i][j])) {
          string nows = solve(i, j);
          if (ismax(nows, ans))
            ans = nows;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int w, h;
char c[71][71];
string ans;

bool compare(string s1, string s2) {
  // cout << s1 << " ";
  while (s1[0] == '0' && s1 != "") {
    s1 = s1.substr(1);
  }
  while (s2[0] == '0' && s2 != "") {
    s2 = s2.substr(1);
  }
  if (s2 == "")
    s2 = "0";
  // cout << s1 << endl;
  int l1 = s1.length(), l2 = s2.length();
  if (l1 > l2)
    return true;
  else if (l1 < l2)
    return false;
  else {
    for (int i = 0; i < l1; i++) {
      if (s1[i] > s2[i])
        return true;
      else if (s1[i] < s2[i])
        return false;
    }
  }
  return true;
}

void solve(int x, int y, string now) {
  // cout << now << endl;
  bool isend = true;
  if (x + 1 < h && isdigit(c[x + 1][y])) {
    solve(x + 1, y, now + c[x + 1][y]);
    isend = false;
  }
  if (y + 1 < w && isdigit(c[x][y + 1])) {
    solve(x, y + 1, now + c[x][y + 1]);
    isend = false;
  }
  if (isend && compare(now, ans))
    ans = now;
  return;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> c[i][j];
      }
    }
    ans = "";
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (isdigit(c[i][j])) {
          string now = "";
          now += c[i][j];
          solve(i, j, now);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}