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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, N) for (ll i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

int n;
int h, w;
string table[50];

int main() {
  cin >> n;
  rep(kk, n) {
    cin >> h >> w;
    vector<char> vi;
    rep(i, h) {
      cin >> table[i];
      rep(j, w) {
        if (table[i][j] != '.')
          vi.push_back(table[i][j]);
      }
    }
    sort(ALL(vi));
    vi.erase(unique(ALL(vi)), vi.end());
    bool f = 0;
    ll mci[4][26] = {};
    rep(i, 26) {
      mci[2][i] = h - 1;
      mci[3][i] = w - 1;
    }
    set<char> sc;
    rep(i, vi.size()) {
      rep(j, h) {
        rep(k, w) {
          if (table[j][k] != '.') {
            mci[0][table[j][k] - 'A'] = max(mci[0][table[j][k] - 'A'], j);
            mci[1][table[j][k] - 'A'] = max(mci[1][table[j][k] - 'A'], k);
            mci[2][table[j][k] - 'A'] = min(mci[2][table[j][k] - 'A'], j);
            mci[3][table[j][k] - 'A'] = min(mci[3][table[j][k] - 'A'], k);
          }
        }
      }
      rep(j, vi.size()) {
        bool ok = 1;
        // rep(i, 4) {
        //	cout << mci[2][vi[j] - 'A'] << " " << mci[0][vi[j] - 'A'] << " "
        //		<< mci[3][vi[j] - 'A'] << " " << mci[1][vi[j] - 'A'] <<
        //endl;
        // }
        for (int k = mci[2][vi[j] - 'A']; k <= mci[0][vi[j] - 'A']; k++) {
          for (int l = mci[3][vi[j] - 'A']; l <= mci[1][vi[j] - 'A']; l++) {
            if (table[k][l] != vi[j]) {
              if (sc.find(table[k][l]) == sc.end())
                ok = 0;
            }
          }
        }
        if (ok)
          sc.insert(vi[j]);
      }
    }
    if (sc.size() == vi.size())
      cout << "SAFE" << endl;
    else
      cout << "SUSPICIOUS" << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, N) for (ll i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

int n;
int h, w;
string table[50];

int main() {
  cin >> n;
  rep(kk, n) {
    cin >> h >> w;
    vector<char> vi;
    rep(i, h) {
      cin >> table[i];
      rep(j, w) {
        if (table[i][j] != '.')
          vi.push_back(table[i][j]);
      }
    }
    sort(ALL(vi));
    vi.erase(unique(ALL(vi)), vi.end());
    bool f = 0;
    do {
      map<char, int> mci;
      mci['.'] = 0;
      rep(i, vi.size()) { mci[vi[i]] = i + 1; }
      bool ng = 0;
      rep(i, vi.size()) {
        ll l = w - 1, r = 0, d = h - 1, u = 0;
        rep(j, h) {
          rep(k, w) {
            if (table[j][k] == vi[i]) {
              // cout << j << " " << k << endl;
              l = min(l, k);
              r = max(r, k);
              d = min(d, j);
              u = max(u, j);
            }
          }
        }
        // cout << d << " " << u << " " << l << " " << r << endl;
        for (int j = d; j <= u; j++) {
          for (int k = l; k <= r; k++) {
            if (table[j][k] != vi[i]) {
              if (mci[table[j][k]] < mci[vi[i]])
                ng = 1;
            }
            if (ng)
              break;
          }
          if (ng)
            break;
        }
        if (ng)
          break;
      }
      if (!ng)
        f = 1;
    } while (next_permutation(ALL(vi)));
    if (f)
      cout << "SAFE" << endl;
    else
      cout << "SUSPICIOUS" << endl;
  }
}