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

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define loop for (;;)
#define trace(var) cerr << ">>> " << #var << " = " << var << endl;
#define all(v) begin(v), end(v)
#define pb push_back
#define inf (1e9)
#define eps (1e-9)

typedef long long Integer;
typedef long double Real;
const Real PI = acosl(-1);
typedef pair<int, int> P;

template <class S, class T>
inline ostream &operator<<(ostream &os, pair<S, T> p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

template <class S, class T, class U>
inline ostream &operator<<(ostream &os, tuple<S, T, U> t) {
  return os << '(' << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t)
            << ')';
}

template <class T> inline ostream &operator<<(ostream &os, set<T> v) {
  os << "(set";
  for (T item : v)
    os << ' ' << item;
  os << ")";
  return os;
}

template <class T> inline ostream &operator<<(ostream &os, vector<T> v) {
  if (v.size() == 0) {
    return os << "(empty)";
  }
  os << v[0];
  for (int i = 1, len = v.size(); i < len; ++i)
    os << ' ' << v[i];
  return os;
}

template <class T> inline istream &operator>>(istream &is, vector<T> &v) {
  rep(i, v.size()) is >> v[i];
  return is;
}

//           ^   >  v   <
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<bool> vb;

char f[100][100];
string s[100][100];

bool comp(string s, string t) {
  if (s.size() != t.size())
    return (s.size() < t.size());
  rep(i, s.size()) if (s[i] != t[i]) return (s[i] < t[i]);
  return true;
}

int main() {

  int w, h;
  while (cin >> w >> h, w and h) {
    rep(i, h) rep(j, w) cin >> f[i][j];
    rep(i, h) rep(j, w) s[i][j] = "";

    rep(i, h) {
      rep(j, w) {
        char c = f[i][j];
        if (not isdigit(c))
          continue;
        if (i == 0 and j == 0) {
          if (c != '0')
            s[i][j] += c;
        } else if (i == 0) {
          s[i][j] = s[i][j - 1] + c;
          if (s[i][j] == "0")
            s[i][j] = "";
        } else if (j == 0) {
          s[i][j] = s[i - 1][j] + c;
          if (s[i][j] == "0")
            s[i][j] = "";
        } else {
          if (comp(s[i - 1][j], s[i][j - 1])) {
            s[i][j] = s[i][j - 1] + f[i][j];
          } else {
            s[i][j] = s[i - 1][j] + f[i][j];
          }
          if (s[i][j] == "0")
            s[i][j] = "";
        }
      }
    }

    string ans = "";
    rep(i, h) {
      rep(j, w) {
        if (comp(ans, s[i][j]))
          ans = s[i][j];
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define loop for (;;)
#define trace(var) cerr << ">>> " << #var << " = " << var << endl;
#define all(v) begin(v), end(v)
#define pb push_back
#define inf (1e9)
#define eps (1e-9)

typedef long long Integer;
typedef long double Real;
const Real PI = acosl(-1);
typedef pair<int, int> P;

template <class S, class T>
inline ostream &operator<<(ostream &os, pair<S, T> p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

template <class S, class T, class U>
inline ostream &operator<<(ostream &os, tuple<S, T, U> t) {
  return os << '(' << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t)
            << ')';
}

template <class T> inline ostream &operator<<(ostream &os, set<T> v) {
  os << "(set";
  for (T item : v)
    os << ' ' << item;
  os << ")";
  return os;
}

template <class T> inline ostream &operator<<(ostream &os, vector<T> v) {
  if (v.size() == 0) {
    return os << "(empty)";
  }
  os << v[0];
  for (int i = 1, len = v.size(); i < len; ++i)
    os << ' ' << v[i];
  return os;
}

template <class T> inline istream &operator>>(istream &is, vector<T> &v) {
  rep(i, v.size()) is >> v[i];
  return is;
}

//           ^   >  v   <
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<bool> vb;

char f[100][100];
bool usage[100][100];

bool comp(string s, string t) {
  if (s.size() != t.size())
    return (s.size() < t.size());
  rep(i, s.size()) if (s[i] != t[i]) return (s[i] < t[i]);
  return true;
}

int main() {

  int w, h;
  while (cin >> w >> h, w and h) {
    rep(i, h) {
      rep(j, w) { cin >> f[i][j]; }
    }
    rep(i, h) {
      rep(j, w) { usage[i][j] = false; }
    }

    string ans = "";
    rep(i, h) {
      rep(j, w) {
        if (usage[i][j])
          continue;
        char c = f[i][j];
        if (isdigit(c) and c != '0') {
          stack<pair<P, string>> s;
          s.push(make_pair(make_pair(i, j), ""));

          while (not s.empty()) {
            auto p = s.top();
            s.pop();
            int i = p.first.first;
            int j = p.first.second;
            string str = p.second + f[i][j];
            usage[i][j] = true;
            bool ap = false;
            if (i < h - 1 and isdigit(f[i + 1][j])) {
              s.push(make_pair(make_pair(i + 1, j), str));
              ap = true;
            }
            if (j < w - 1 and isdigit(f[i][j + 1])) {
              s.push(make_pair(make_pair(i, j + 1), str));
              ap = true;
            }
            if (not ap) {
              if (comp(ans, str))
                ans = str;
            }
          }
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}