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

// TLE

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

typedef pair<int, int> pii;

vector<pii> refresh(vector<pii> v) {
  int n = v.size();
  vector<bool> f(n, true);
  REP(i, n) {
    REP(j, n) {
      if (i == j || !f[j])
        continue;
      if (v[i].first <= v[j].first && v[i].second <= v[j].second) {
        f[i] = false;
        break;
      }
    }
  }
  vector<pii> res;
  REP(i, n)
  if (f[i])
    res.push_back(v[i]);
  return res;
}

int main() {
  int h, w;
  while (cin >> h >> w, w + h) {
    char ba[w][h];
    REP(y, h)
    REP(x, w)
    cin >> ba[x][y];
    vector<pii> dp[w][h];
    int ans = 0;
    REP(y, h) {
      REP(x, w) {
        if (ba[x][y] == '.') {
          int ue = 0, hidari = 0;
          for (int i = y - 1; i >= 0 && ba[x][i] == '.'; i--)
            ue++;
          for (int i = x - 1; i >= 0 && ba[i][y] == '.'; i--)
            hidari++;
          vector<pii> v;
          if (ue > 0 && hidari > 0) {
            FOR(it, dp[x - 1][y]) {
              v.push_back(pii(it->first + 1, min(ue + 1, it->second)));
            }
            FOR(it, dp[x][y - 1]) {
              v.push_back(pii(min(hidari + 1, it->first), it->second + 1));
            }
            v.push_back(pii(1, ue + 1));
            v.push_back(pii(hidari + 1, 1));
          } else {
            v.push_back(pii(hidari + 1, ue + 1));
          }
          dp[x][y] = refresh(v);
          //  cout << hidari << " "<<ue;

          FOR(it, dp[x][y]) {
            //  printf("(%d,%d)",it->first,it->second);
            ans = max(ans, it->first * it->second);
          }
        }
        //    cout << "_";
      }
      // cout << endl;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

typedef pair<int, int> pii;
int main() {
  int h, w;
  while (cin >> h >> w, w + h) {
    char ba[w][h];
    REP(y, h)
    REP(x, w)
    cin >> ba[x][y];
    vector<pii> dp[w][h];
    int ans = 0;
    REP(y, h) {
      REP(x, w) {
        if (ba[x][y] == '.') {
          set<pii> tmpset;
          int ue = 0, hidari = 0;
          for (int i = y - 1; i >= 0 && ba[x][i] == '.'; i--)
            ue++;
          for (int i = x - 1; i >= 0 && ba[i][y] == '.'; i--)
            hidari++;
          if (ue > 0 && hidari > 0) {
            FOR(it, dp[x - 1][y]) {
              tmpset.insert(pii((*it).first + 1, min(ue + 1, (*it).second)));
            }
            FOR(it, dp[x][y - 1]) {
              tmpset.insert(
                  pii(min(hidari + 1, (*it).first), (*it).second + 1));
            }
            tmpset.insert(pii(1, ue + 1));
            tmpset.insert(pii(hidari + 1, 1));
          } else {
            tmpset.insert(pii(hidari + 1, ue + 1));
          }
          // cout << hidari << " "<<ue;
          FOR(it, tmpset) {
            int f = 0;
            FOR(it2, dp[x][y]) {
              if ((*it).first <= (*it2).first &&
                  (*it).second <= (*it2).second) {
                f = 1;
                break;
              }
            }
            dp[x][y].push_back(*it);
            // printf("(%d,%d)",(*it).first,(*it).second);
            ans = max(ans, (*it).first * (*it).second);
          }
        }
        // cout << " ";
      }
      // cout << endl;
    }
    cout << ans << endl;
  }
}