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

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

bool isnum(char a) { return (a >= '0' && a <= '9'); }
string removezero(string a) {
  string rst = "";
  bool flg = false;
  REP(i, a.size()) {
    if (flg) {
      rst += a[i];
    } else {
      if (a[i] != '0') {
        flg = true;
        rst += a[i];
      }
    }
  }
  return rst;
}
string getbiggerstr(string a, string b) {
  a = removezero(a);
  b = removezero(b);
  if (a.size() < b.size())
    return b;
  else if (a.size() > b.size())
    return a;
  REP(i, a.size()) {
    if (a[i] < b[i]) {
      return b;
    } else if (a[i] > b[i]) {
      return a;
    }
  }
  return a;
}
int main() {
  int w, h;
  char g[70][70];
  string s[70][70];
  // cout << getbig("23900","23114") << endl;
  while (1) {
    string ans = "";
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    REP(i, h) REP(j, w) cin >> g[i][j];
    REP(i, h) REP(j, w) s[i][j] = "";
    REP(i, h) {
      REP(j, w) {
        if (!isnum(g[i][j]))
          continue;
        int lx = i;
        int ly = j - 1;
        int ux = i - 1;
        int uy = j;
        string l, r;
        if (lx >= 0 && ly >= 0)
          l = s[lx][ly];
        else
          l = "";
        if (ux >= 0 && uy >= 0)
          r = s[ux][uy];
        else
          r = "";
        l.pb(g[i][j]);
        r.pb(g[i][j]);
        s[i][j] = getbiggerstr(l, r);
        // cout << l << "??¨" << r << " big " << s[i][j] << endl;
        ans = getbiggerstr(s[i][j], ans);
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int w, h;
vector<string> data;
int dx[2] = {1, 0};
int dy[2] = {0, 1};
bool sudeni[100][100];

bool inrange(int x, int y) {
  if (x < 0 || y < 0 || x > h - 1 || y > w - 1)
    return false;
  else
    return true;
}
string rmzero(string a) {
  string str = "";
  bool f = false;
  REP(i, a.size()) {
    if (!f) {
      if (a[i] != '0') {
        str += a[i];
        f = true;
      }
    } else {
      str += a[i];
    }
  }
  return str;
}

string getbigger(string a, string b, bool ff) {
  if (ff) {
    a = rmzero(a);
    b = rmzero(b);
  }
  if (a.size() != b.size()) {
    if (a.size() > b.size())
      return a;
    else
      return b;
  } else {
    REP(i, a.size()) {
      if (a[i] == b[i])
        continue;
      if (a[i] > b[i])
        return a;
      else
        return b;
    }
    return a; // eq
  }
}

string solve(int px, int py) {
  string rst = "";
  if (!inrange(px, py))
    return "";
  sudeni[px][py] = true;
  if ('0' <= data[px][py] && data[px][py] <= '9') {
    rst += data[px][py];
  }
  vector<string> tmp;
  REP(i, 2) {
    tmp.pb("");
    int nx = px + dx[i];
    int ny = py + dy[i];
    if (inrange(nx, ny) && '0' <= data[nx][ny] && data[nx][ny] <= '9') {
      tmp[i] = solve(nx, ny);
    }
  }
  rst = getbigger(rst + tmp[0], rst + tmp[1], false);
  return rst;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    data.clear();
    REP(i, h) {
      string tmp;
      cin >> tmp;
      data.pb(tmp);
    }
    string ans = "0";
    REP(i, 100) REP(j, 100) sudeni[i][j] = false;
    REP(i, h) {
      REP(j, w) {
        if (sudeni[i][j] == false && data[i][j] >= '1' && data[i][j] <= '9') {
          string rst = solve(i, j);
          // cout << rst << endl;
          ans = getbigger(rst, ans, true);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}