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

#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define debug(x) cerr << #x << ": " << x << endl;

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct edge {
  int from, to;
  ll cost;
};

map<char, bool> is_removed;
int baggage_cnt = 0;
int h, w;
char c = '*';

bool is_rectangle(int si, int sj, int gi, int gj, char c, string s[]) {

  rep(h) {
    repp(j, w) {
      if (si <= i && i <= gi && sj <= j && j <= gj) {
        if (s[i][j] != c && !is_removed[s[i][j]]) {
          return false;
        }
      } else {
        if (s[i][j] == c)
          return false;
      }
    }
  }
  return true;
}

string solve(string s[]) {
  set<char> st;
  rep(h) {
    repp(j, w) {
      if (s[i][j] != '.')
        st.insert(s[i][j]);
    }
  }
  baggage_cnt = (int)st.size();
  if (!baggage_cnt)
    return "SAFE";

  bool change = true;
  while (change) {
    change = false;
    for (char c : st) {
      if (is_removed[c])
        st.erase(c);
    }
    for (char c : st) {
      int si = INT_MAX, sj = INT_MAX, gi = -1, gj = -1;
      rep(h) {
        repp(j, w) {
          if (s[i][j] == c) {
            si = min(i, si);
            sj = min(j, sj);
            gi = max(i, gi);
            gj = max(j, gj);
          }
        }
      }
      if (gi == -1)
        continue;

      if (is_rectangle(si, sj, gi, gj, c, s)) {
        is_removed[c] = true;
        change = true;
        baggage_cnt--;
        if (!baggage_cnt)
          return "SAFE";
      }
    }
  }
  return "SUSPICIOUS";
}

int main() {
  int t;
  cin >> t;

  repp(tt, t) {
    cin >> h >> w;
    string s[h];
    rep(h) cin >> s[i];
    is_removed.clear();
    baggage_cnt = 0;
    cout << solve(s) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define debug(x) cerr << #x << ": " << x << endl;

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct edge {
  int from, to;
  ll cost;
};

map<char, bool> is_removed;
int baggage_cnt = 0;
int h, w;
char c = '*';

bool is_rectangle(int si, int sj, int gi, int gj, char c, string s[]) {

  rep(h) {
    repp(j, w) {
      if (si <= i && i <= gi && sj <= j && j <= gj) {
        if (s[i][j] != c && !is_removed[s[i][j]]) {
          return false;
        }
      } else {
        if (s[i][j] == c)
          return false;
      }
    }
  }
  return true;
}

string solve(string s[]) {
  set<char> st;
  rep(h) {
    repp(j, w) {
      if (s[i][j] != '.')
        st.insert(s[i][j]);
    }
  }
  baggage_cnt = (int)st.size();

  while (true) {
    for (char c : st) {
      if (is_removed[c])
        st.erase(c);
    }
    for (char c : st) {
      int si = -1, sj = -1, gi = -1, gj = -1;
      rep(h) {
        repp(j, w) {
          if (s[i][j] == c) {
            if (si == -1) {
              si = i;
              sj = j;
              gi = i;
              gj = j;
            } else {
              si = min(i, si);
              sj = min(j, sj);
              gi = max(i, gi);
              gj = max(j, gj);
            }
          }
        }
      }
      if (si == -1)
        continue;

      if (is_rectangle(si, sj, gi, gj, c, s)) {
        is_removed[c] = true;
        baggage_cnt--;
        if (!baggage_cnt)
          return "SAFE";
      }
    }
  }
  return "SUSPICIOUS";
}

int main() {
  int t;
  cin >> t;

  repp(tt, t) {
    cin >> h >> w;
    string s[h];
    rep(h) cin >> s[i];
    is_removed.clear();
    baggage_cnt = 0;
    cout << solve(s) << endl;
  }
}