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


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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

int W, H;
int dw[2] = {0, 1};
int dh[2] = {1, 0};
vector<vector<char>> matrix;
vector<vector<string>> dp;

string max(string str1, string str2) {
  if (str1.length() == str2.length()) {
    rep(i, str1.size()) {
      if (str1[i] != str2[i]) {
        return str1[i] > str2[i] ? str1 : str2;
      }
    }
    return str1;
  } else {
    return str1.length() > str2.length() ? str1 : str2;
  }
}

string dfs(int w, int h) {
  if (dp[h][w] != "#") {
    return dp[h][w];
  }

  string res = "";
  rep(i, 2) {
    int nw = w + dw[i];
    int nh = h + dh[i];
    if (nw < 0 || nw >= W || nh < 0 || nh >= H) {
      continue;
    }
    if (!isdigit(matrix[nh][nw])) {
      continue;
    }
    res = max(res, dfs(nw, nh));
  }

  dp[h][w] = matrix[h][w] + res;
  return dp[h][w];
}

int main() {
  while (cin >> W >> H && W) {
    matrix.clear();
    matrix.resize(H, vector<char>(W));

    rep(h, H) {
      rep(w, W) { cin >> matrix[h][w]; }
    }

    dp.clear();
    dp.resize(H, vector<string>(W, "#"));

    string ans;
    rep(h, H) {
      rep(w, W) {
        if (isdigit(matrix[h][w]) && matrix[h][w] != '0') {
          ans = max(ans, dfs(w, h));
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:


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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

int W, H;
int dw[2] = {0, 1};
int dh[2] = {1, 0};
string ans;

bool cmp(string str1, string str2) {
  if (str1.length() == str2.length()) {
    rep(i, str1.size()) {
      if (str1[i] != str2[i]) {
        return str1[i] < str2[i];
      }
    }
    return false;
  } else if (str1.length() < str2.length()) {
    return true;
  }

  return false;
}

vector<char> adjust(vector<char> list) {
  vector<char> res;
  bool flag = false;
  rep(i, list.size()) {
    if (list[i] != '0' || flag) {
      res.push_back(list[i]);
      flag = true;
    }
  }

  return res;
}

void dfs(int w, int h, vector<char> list, vector<vector<char>> &matrix) {
  if (w < 0 || w >= W || h < 0 || h >= H) {
    return;
  }
  if (!isdigit(matrix[h][w])) {
    return;
  }

  list.push_back(matrix[h][w]);

  rep(i, 2) { dfs(w + dw[i], h + dh[i], list, matrix); }

  if (list.size() == 0) {
    return;
  }
  list = adjust(list);
  string num(ALL(list));

  if (cmp(ans, num)) {
    ans = num;
  }
  return;
}

int main() {
  while (cin >> W >> H && W) {

    vector<vector<char>> matrix(H, vector<char>(W));

    rep(h, H) {
      rep(w, W) { cin >> matrix[h][w]; }
    }

    ans = "";
    vector<char> list;
    rep(h, H) {
      rep(w, W) { dfs(w, h, list, matrix); }
    }

    cout << ans << endl;
  }

  return 0;
}