alexjercan/bugnet huggingface dataset:
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
const int INF = 100000000;
using namespace std;

const int n = 75;
char m[n][n];
string memo[n][n];
string ans;

string big(string a, string b) {
  if (a.size() > b.size()) {
    return a;
  } else if (a.size() < b.size()) {
    return b;
  } else {
    return a > b ? a : b;
  }
}

string dfs(int x, int y) {
  string ret;
  if (memo[y][x] != "")
    return memo[y][x];

  if (not isdigit(m[y][x]))
    return ret;
  else
    ret += m[y][x];

  return memo[y][x] = ret + big(dfs(x + 1, y), dfs(x, y + 1));
}

int main() {
  int w, h;
  while (cin >> w >> h, w || h) {
    ans = "";
    rep(i, n) rep(j, n) memo[i][j] = "";
    rep(i, n) rep(j, n) m[i][j] = '#';
    rep(i, h) rep(j, w) cin >> m[i + 1][j + 1];
    rep(i, h) {
      rep(j, w) {
        if (isdigit(m[i + 1][j + 1]) && m[i + 1][j + 1] != '0') {
          ans = big(ans, dfs(j + 1, i + 1));
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
const int INF = 100000000;
using namespace std;

char m[75][75];
string memo[75][75];
string ans;

string big(string a, string b) {
  if (a.size() > b.size()) {
    return a;
  } else if (a.size() < b.size()) {
    return b;
  } else {
    return a > b ? a : b;
  }
}

void dfs(string a, int x, int y) {
  if (a.size() != 0 || m[y][x] != '0')
    a += m[y][x];
  if (big(memo[y][x], a) == memo[y][x])
    return;
  else if (memo[y][x] == a)
    return;
  else {
    memo[y][x] = a;
  }
  if (isdigit(m[y][x + 1])) {
    dfs(a, x + 1, y);
  }
  if (isdigit(m[y + 1][x])) {
    dfs(a, x, y + 1);
  }
  ans = big(ans, a);
}

int main() {
  int w, h;
  while (cin >> w >> h, w || h) {
    ans = "";
    rep(i, h) rep(j, w) memo[i][j] = "";
    rep(i, h) rep(j, w) m[i + 1][j + 1] = '#';
    rep(i, h) rep(j, w) cin >> m[i + 1][j + 1];
    rep(i, h) {
      rep(j, w) {
        if (isdigit(m[i][j]) && memo[i][j] == "")
          dfs("", j, i);
      }
    }
    cout << ans << endl;
  }
}
Problem p00707 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int main(void) {
  for (;;) {
    int w, h;
    cin >> w >> h;
    if (!w && !h)
      return 0;
    vector<string> field;
    field.resize(h);
    REP(i, h) cin >> field[i];
    vector<vector<string>> dp;
    dp.resize(h + 1);
    REP(i, h + 1) dp[i].assign(w + 1, "");
    REP(i, h) REP(j, w) {
      if (0 <= i && i < h && 0 <= j && j < w) {
        if (('1' <= field[i][j] && field[i][j] <= '9') ||
            (field[i][j] == '0' && dp[i][j] != ""))
          dp[i][j] += field[i][j];
        else
          dp[i][j] = "";
        if ((dp[i + 1][j].size() < dp[i][j].size()) ||
            (dp[i + 1][j].size() == dp[i][j].size() &&
             dp[i + 1][j] < dp[i][j] + field[i][j]))
          dp[i + 1][j] = dp[i][j];
        if ((dp[i][j + 1].size() < dp[i][j].size()) ||
            (dp[i][j + 1].size() == dp[i][j].size() &&
             dp[i][j + 1] < dp[i][j] + field[i][j]))
          dp[i][j + 1] = dp[i][j];
      }
    }
    string answer = "0";
    REP(i, h + 1)
    REP(j, w + 1) if (dp[i][j].size() > answer.size() ||
                      (dp[i][j].size() == answer.size() && dp[i][j] > answer))
        answer = dp[i][j];

    cout << answer << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int w, h;
vector<string> field;
vector<vector<bool>> check;
vector<string> code;
void createcode(int i, int j, string tmp) {
  if (0 <= i && i < h && 0 <= j && j < w) {
    check[i][j] = true;
    if ('0' <= field[i][j] && field[i][j] <= '9') {
      string nextmp = tmp;
      nextmp.push_back(field[i][j]);
      createcode(i + 1, j, nextmp);
      createcode(i, j + 1, nextmp);
    }
  } else
    code.push_back(tmp);
}

int main(void) {
  for (;;) {
    cin >> w >> h;
    if (!w && !h)
      return 0;
    field.resize(h);
    REP(i, h) cin >> field[i];
    check.resize(h);
    REP(i, h) check[i].assign(w, false);
    code.clear();
    REP(i, h) REP(j, w) {
      if (!check[i][j]) {
        check[i][j] = true;
        if ('1' <= field[i][j] && field[i][j] <= '9')
          createcode(i, j, "");
      }
    }
    string answer = "0";
    REP(i, code.size()) {
      if (code[i].size() > answer.size() ||
          (code[i].size() == answer.size() && code[i] > answer))
        answer = code[i];
    }
    cout << answer << endl;
  }
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int W, H;
vector<string> str;
bool used[70][70];
string memo[70][70];
const int dx[] = {1, 0};
const int dy[] = {0, 1};

bool Comp(const string &l, const string &r) {
  return l.size() == r.size() ? l < r : l.size() < r.size();
}

string MAX(const string &l, const string &r) { return Comp(l, r) ? r : l; }

string dfs(int x, int y) {
  if (memo[x][y] != "")
    return memo[x][y];
  string res[2];
  for (int i = 0; i < 2; i++) {
    int tx = x + dx[i], ty = y + dy[i];
    if (tx >= 0 && tx < H && ty >= 0 && ty < W && str[tx][ty] >= '0' &&
        str[tx][ty] <= '9') {
      res[i] = dfs(tx, ty);
    }
  }
  used[x][y] = true;
  return memo[x][y] = str[x][y] + MAX(res[0], res[1]);
}

int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        used[i][j] = false;
        memo[i][j].clear();
      }
    }
    str.resize(H);
    for (int i = 0; i < H; i++) {
      cin >> str[i];
    }
    string res;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (!used[i][j] && str[i][j] >= '1' && str[i][j] <= '9') {
          res = MAX(res, dfs(i, j));
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int W, H;
vector<string> str;
bool used[70][70];
const int dx[] = {1, 0};
const int dy[] = {0, 1};

bool Comp(const string &l, const string &r) {
  return l.size() == r.size() ? l < r : l.size() < r.size();
}

string MAX(const string &l, const string &r) { return Comp(l, r) ? r : l; }

string dfs(int x, int y) {
  string res;
  for (int i = 0; i < 2; i++) {
    int tx = x + dx[i], ty = y + dy[i];
    if (tx >= 0 && tx < H && ty >= 0 && ty < W && str[tx][ty] >= '0' &&
        str[tx][ty] <= '9') {
      res = MAX(res, dfs(tx, ty));
    }
  }
  used[x][y] = true;
  return str[x][y] + res;
}

int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        used[i][j] = false;
      }
    }
    str.resize(H);
    for (int i = 0; i < H; i++) {
      cin >> str[i];
    }
    string res;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (!used[i][j] && str[i][j] >= '1' && str[i][j] <= '9') {
          res = MAX(res, dfs(i, j));
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cctype>
#include <iostream>
using namespace std;

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

int main() {
  int W, H;
  char C[70][70];
  string max, dp[70][70];
  while (cin >> W >> H, W | H) {
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        cin >> C[i][j];
        dp[i][j] = "";
      }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (isalpha(C[i][j]))
          continue;
        string above, left;
        above = left = C[i][j];
        if (i > 0 && dp[i - 1][j] != "0")
          above = dp[i - 1][j] + C[i][j];
        if (j > 0 && dp[i][j - 1] != "0")
          left = dp[i][j - 1] + C[i][j];
        if (compare(above, left)) {
          dp[i][j] = above;
        } else {
          dp[i][j] = left;
        }
      }
    }
    max = "";
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        if (compare(dp[i][j], max))
          max = dp[i][j];
    cout << max << endl;
  }
}

Error: TLE

Fixed submission:

#include <cctype>
#include <iostream>
using namespace std;

int W, H;
string ans;
char matrix[70][70];
int dx[2] = {1, 0};
int dy[2] = {0, 1};
bool visited[70][70];

void rec(int y, int x, string s) {
  visited[y][x] = 1;
  for (int i = 0; i < 2; i++) {
    int xx = x + dx[i], yy = y + dy[i];
    if (xx < 0 || yy < 0)
      continue;
    if (!isdigit(matrix[yy][xx]))
      continue;
    string t = s;
    t += matrix[yy][xx];
    rec(yy, xx, t);
  }
  if (ans.size() < s.size())
    ans = s;
  else if (ans.size() == s.size()) {
    if (ans < s)
      ans = s;
  }
  return;
}

int main() {
  int i, j;
  while (cin >> W >> H, W | H) {
    for (i = 0; i < H; i++) {
      for (j = 0; j < W; j++) {
        cin >> matrix[i][j];
        visited[i][j] = 0;
      }
    }
    ans = "";
    for (i = 0; i < H; i++) {
      for (j = 0; j < W; j++) {
        if (isdigit(matrix[i][j]) && !visited[i][j] && matrix[i][j] != '0') {
          string t = "";
          t += matrix[i][j];
          rec(i, j, t);
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;

bool isN(char c) { return '0' <= c && c <= '9'; }

bool comp(string s1, string s2) {
  if (s1.length() < s2.length()) {
    for (int i = 0; i < s2.length() - s1.length() + 1; i++) {
      s1 = '0' + s1;
    }
  } else if (s1.length() > s2.length()) {
    for (int i = 0; i < s1.length() - s2.length() + 1; i++) {
      s2 = '0' + s2;
    }
  }
  return (s1 < s2);
}

int main() {
  int w, h;
  while (cin >> w >> h, w || h) {
    vector<string> f(h);
    rep(i, h) cin >> f[i];
    string dp[70][70];
    rep(x, w) rep(y, h) dp[y][x] = "";
    if (isN(f[0][0]))
      dp[0][0] += f[0][0];
    string res = "";
    rep(y, h) {
      rep(x, w) {
        if (isN(f[y][x])) {
          string con = "";
          if (x - 1 >= 0 && comp(con, dp[y][x - 1]))
            con = dp[y][x - 1];
          if (y - 1 >= 0 && comp(con, dp[y - 1][x]))
            con = dp[y - 1][x];
          dp[y][x] = con + f[y][x];
        }
        if (comp(res, dp[y][x]))
          res = dp[y][x];
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;

int w, h;
char f[71][71];
string memo[71][71];

bool isNumber(char c) { return ('0' <= c && c <= '9'); }

int comp(string l, string r) {
  reverse(l.begin(), l.end());
  reverse(r.begin(), r.end());
  while (l.length() < r.length())
    l += '0';
  while (r.length() < l.length())
    r += '0';
  reverse(l.begin(), l.end());
  reverse(r.begin(), r.end());
  return l > r ? 1 : l < r ? -1 : 0;
}

void dfs(int x, int y, string &path) {
  if (memo[x][y] != "") {
    path += memo[x][y];
    return;
  }
  string retPath = "";
  if (x + 1 < w && isNumber(f[x + 1][y])) {
    string p = "";
    p += f[x + 1][y];
    dfs(x + 1, y, p);
    if (comp(p, retPath) > 0) {
      retPath = p;
    }
  }
  if (y + 1 < h && isNumber(f[x][y + 1])) {
    string p = "";
    p += f[x][y + 1];
    dfs(x, y + 1, p);
    if (comp(p, retPath) > 0) {
      retPath = p;
    }
  }
  path += retPath;
  memo[x][y] = retPath;
}

int main() {
  while (cin >> w >> h, w || h) {
    memset(f, 0, sizeof(f));
    rep(y, h) {
      rep(x, w) {
        cin >> f[x][y];
        memo[x][y] = "";
      }
    }
    string mPath = "";
    rep(y, h) {
      rep(x, w) {
        if (isNumber(f[x][y])) {
          string path = "";
          path += f[x][y];
          dfs(x, y, path);
          if (comp(path, mPath) > 0) {
            mPath = path;
          }
        }
      }
    }
    int ind = 0;
    while (mPath[ind] == '0')
      ind++;
    cout << mPath.substr(ind, mPath.length() - ind) << endl;
  }
  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define reep(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) reep((i), 0, (n))
string ans = "";
string tmp = "";
vector<string> v;
vector<vector<int>> vv;
vector<vector<string>> dp;
int w, h;
bool check(int a, int b) {
  if (0 <= a && a < h && 0 <= b && b < w)
    return true;
  return false;
}
string cmp(string a, string b) {
  if (a.size() < b.size())
    return b;
  if (a.size() > b.size())
    return a;
  if (a > b)
    return a;
  return b;
}
string foo(int a, int b) {
  // tmp+=v[a].substr(b,1);
  if (vv[a][b])
    return dp[a][b];
  vv[a][b] = 1;
  // if(ans.size()<tmp.size()) ans=tmp;
  // else if(ans.size()==tmp.size()&&ans<tmp) ans=tmp;
  string ret = "";
  if (check(a + 1, b) && isdigit(v[a + 1][b]))
    ret = cmp(ret, foo(a + 1, b));
  if (check(a, b + 1) && isdigit(v[a][b + 1]))
    ret = cmp(ret, foo(a, b + 1));
  // tmp=tmp.substr(0,tmp.size()-1);
  return dp[a][b] = v[a].substr(b, 1) + ret;
}
int main() {
  while (cin >> w >> h, h || w) {
    v.clear();
    v = vector<string>(h);
    vv = vector<vector<int>>(h, vector<int>(w, 0));
    dp = vector<vector<string>>(h, vector<string>(w, ""));
    rep(i, h) { cin >> v[i]; }
    ans = "";
    rep(i, h) {
      rep(j, w) {
        if (isdigit(v[i][j]) && v[i][j] != '0') {
          if (vv[i][j])
            continue;
          ans = cmp(ans, foo(i, j));
          // cout<<i<<" "<<j<<" "<<v[i][j]<<" "<<ans<<endl;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define reep(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) reep((i), 0, (n))
string ans = "";
string tmp = "";
vector<string> v;
vector<vector<int>> vv;
int w, h;
bool check(int a, int b) {
  if (0 <= a && a < h && 0 <= b && b < w)
    return true;
  return false;
}
void foo(int a, int b) {
  tmp += v[a].substr(b, 1);
  vv[a][b] = 1;
  if (ans.size() < tmp.size())
    ans = tmp;
  else if (ans.size() == tmp.size() && ans < tmp)
    ans = tmp;
  if (check(a + 1, b) && isdigit(v[a + 1][b]))
    foo(a + 1, b);
  if (check(a, b + 1) && isdigit(v[a][b + 1]))
    foo(a, b + 1);
  tmp = tmp.substr(0, tmp.size() - 1);
}
int main() {
  while (cin >> w >> h, h || w) {
    v.clear();
    v = vector<string>(h);
    vv = vector<vector<int>>(h, vector<int>(w, 0));
    rep(i, h) { cin >> v[i]; }
    ans = tmp = "";
    rep(i, h) {
      rep(j, w) {
        if (isdigit(v[i][j]) && v[i][j] != '0') {
          if (vv[i][j])
            continue;
          foo(i, j);
          // cout<<i<<" "<<j<<" "<<v[i][j]<<" "<<ans<<endl;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00707 (C++) — buggy submission, verdict: Runtime Error


// The Secret Number
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1126

#include <iostream>
#include <vector>

using namespace std;

int W, H;
vector<string> matrix;
vector<vector<string>> memo;

int dx[2] = {1, 0};
int dy[2] = {0, 1};

inline bool is_out_of_range(int x, int y) {
  return (x < 0 || x >= W || y < 0 || y >= H);
}

string cmp(string a, string b) {
  if (a.size() > b.size())
    return a;
  if (a.size() < b.size())
    return b;
  return max(a, b);
}

string dfs(int x, int y) {
  if (is_out_of_range(x, y))
    return "";
  if (!isdigit(matrix[y][x]))
    return "";
  if (memo[y][x] != "")
    return memo[y][x];

  string res = "";
  for (int d = 0; d < 2; ++d) {
    res = cmp(res, matrix[y][x] + dfs(x + dx[d], y + dy[d]));
  }
  memo[y][x] = res;
  return res;
}

int main() {
  while (cin >> W >> H && W) {
    matrix = vector<string>(H, "");
    memo.assign(H, vector<string>(W, ""));
    for (int h = 0; h < H; ++h) {
      cin >> matrix[h];
    }

    string ans = "";
    for (int y = 0; y < H; ++y) {
      for (int x = 0; x < W; ++x) {
        if (matrix[y][x] == '0' || !isdigit(matrix[y][x]))
          continue;
        ans = cmp(ans, dfs(x, y));
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:


// The Secret Number
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1126

#include <iostream>
#include <vector>

using namespace std;

int W, H;
vector<string> matrix;
vector<vector<string>> memo;

int dx[3] = {1, 1, 0};
int dy[3] = {0, 1, 1};

inline bool is_out_of_range(int x, int y) {
  return (x < 0 || x >= W || y < 0 || y >= H);
}

string cmp(string a, string b) {
  if (a.size() > b.size())
    return a;
  if (a.size() < b.size())
    return b;
  return max(a, b);
}

string dfs(int x, int y, string seq) {
  string ans = seq;
  if (!isdigit(matrix[y][x]))
    return seq;
  if (is_out_of_range(x, y))
    return seq;
  if (memo[y][x] != "")
    return memo[y][x];

  for (int d = 0; d < 3; ++d) {
    ans = cmp(ans, dfs(x + dx[d], y + dy[d], seq + matrix[y][x]));
  }
  return ans;
}

int main() {
  while (cin >> W >> H && W) {
    matrix = vector<string>(H, "");
    memo.assign(H, vector<string>(W, ""));
    for (int h = 0; h < H; ++h) {
      cin >> matrix[h];
    }

    string ans = "";
    for (int y = 0; y < H; ++y) {
      for (int x = 0; x < W; ++x) {
        if (matrix[y][x] == '0' || !isdigit(matrix[y][x]))
          continue;
        ans = cmp(ans, dfs(x, y, ""));
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int w, h;
vector<string> data;
vector<vector<string>> str;

string get_large(const string &a, const string &b) {
  if (a.size() != b.size())
    return (a.size() > b.size()) ? a : b;
  for (int i = 0; i < a.size(); ++i) {
    if (a[i] != b[i])
      return (a[i] > b[i]) ? a : b;
  }
  return a;
}

string dfs(int i, int j) {
  if (!(i >= 0 && i < h))
    return string();
  if (!(j >= 0 && j < w))
    return string();
  if (!(data[i][j] >= '0' && data[i][j] <= '9'))
    return string();
  if (str[i][j].empty())
    str[i][j] = data[i][j] + get_large(dfs(i + 1, j), dfs(i, j + 1));
  return str[i][j];
}

string solve() {
  string ans = "0";
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (data[i][j] >= '1' && data[i][j] <= '9' && str[i][j].empty())
        ans = get_large(ans, dfs(i, j));
    }
  }
  return ans;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    data = vector<string>(h);
    str = vector<vector<string>>(h, vector<string>(w));
    for (int i = 0; i < h; ++i)
      cin >> data[i];
    cout << solve() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int w, h;
vector<string> data;
vector<vector<string>> str;

string get_large(const string &a, const string &b) {
  if (a.size() != b.size())
    return (a.size() > b.size()) ? a : b;
  for (int i = 0; i < a.size(); ++i) {
    if (a[i] != b[i])
      return (a[i] > b[i]) ? a : b;
  }
  return a;
}

string dfs(int i, int j) {
  if (!(i >= 0 && i < h))
    return string();
  if (!(j >= 0 && j < w))
    return string();
  if (!(data[i][j] >= '0' && data[i][j] <= '9'))
    return string();
  str[i][j] = data[i][j] + get_large(dfs(i + 1, j), dfs(i, j + 1));
  return str[i][j];
}

string solve() {
  string ans = "0";
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (data[i][j] >= '1' && data[i][j] <= '9' && str[i][j].empty()) {
        ans = get_large(ans, dfs(i, j));
      }
    }
  }
  return ans;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    data = vector<string>(h);
    str = vector<vector<string>>(h, vector<string>(w));
    for (int i = 0; i < h; ++i)
      cin >> data[i];
    cout << solve() << endl;
  }
  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

string s[71];
const int dx[] = {0, 1}, dy[] = {1, 0};
string dp[71][71];
string ans;
int w, h;
bool cmp(const string &a, const string &b) {
  if (a.size() != b.size())
    return a.size() < b.size();
  else {
    rep(i, a.size()) {
      if (a[i] > b[i]) {
        return false;
      } else if (a[i] < b[i])
        return true;
    }
  }
  return false;
}
string dfs(int x, int y) {
  if (dp[y][x] != ".") {
    return dp[y][x];
  }
  string ret;
  ret += s[y][x];
  rep(i, 2) {
    int tx = x + dx[i], ty = y + dy[i];
    if (tx >= 0 && ty >= 0 && tx < w && ty < h && isdigit(s[ty][tx])) {
      ret = max(ret, s[y][x] + dfs(tx, ty), cmp);
    }
  }
  return dp[y][x] = ret;
}
int main() {
  while (cin >> w >> h, w) {
    rep(i, h) rep(j, w) dp[i][j] = ".";
    rep(i, h) cin >> s[i];
    ans = "";
    rep(i, h) rep(j, w) if (isdigit(s[i][j]) && s[i][j] != '0') {
      ans = max(ans, dfs(j, i), cmp);
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

string s[71];
bool used[71][71];
const int dx[] = {0, 1}, dy[] = {1, 0};
int dp[71][71];
string ans;
int w, h;
int dfs(int x, int y, string c) {
  if (dp[y][x] != -1) {
    if (dp[y][x] + c.size() < ans.size())
      return dp[y][x] + c.size();
  }
  if (c.size() > ans.size())
    ans = c;
  else if (c.size() == ans.size()) {
    rep(i, c.size()) {
      if (c[i] > ans[i]) {
        ans = c;
        break;
      } else if ((c[i] < ans[i]))
        break;
    }
  }
  int ret = 1;
  rep(i, 2) {
    int tx = x + dx[i], ty = y + dy[i];
    if (tx >= 0 && ty >= 0 && tx < w && ty < h && isdigit(s[ty][tx])) {
      ret = max(ret, dfs(tx, ty, c + s[ty][tx]) + 1);
    }
  }
  return dp[y][x] = ret;
}
int main() {
  while (cin >> w >> h, w) {
    memset(dp, -1, sizeof(dp));
    rep(i, h) cin >> s[i];
    ans = "";
    string t;
    rep(i, h) rep(j, w) if (isdigit(s[i][j]) && s[i][j] != '0') {
      dfs(j, i, t + s[i][j]);
    }
    cout << ans << endl;
  }
  return 0;
}