Problem p03383 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define DEBUG(x) cout << #x << ": " << x << endl;
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  for (int i = 0; i < v.size(); i++)                                           \
    cout << " " << v[i];                                                       \
  cout << endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define in(x, a, b) a <= x &&x < b
const int inf = 1000000001;
const ll INF = 2e18;
const ll MOD = 1000000007;
// const ll mod = 1000000009;
const double pi = 3.14159265358979323846;
#define Sp(p) cout << setprecision(15) << fixed << p << endl;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx2[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy2[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int h, w;
vector<vector<char>> s;
vi idx;

bool cmp(int i, int j) {
  rep(k, w) {
    if (s[i][idx[k]] != s[j][idx[w - 1 - k]]) {
      return false;
    }
  }
  return true;
}

bool check() {
  vector<bool> used(h, false);
  rep(i, h) {
    if (used[i]) {
      continue;
    }
    for (int j = i + 1; j < h; j++) {
      if (used[j]) {
        continue;
      }
      if (cmp(i, j)) {
        used[i] = used[j] = true;
        break;
      }
    }
  }
  // DEBUG_VEC(used);
  int i0 = -1;
  rep(i, h) {
    if (!used[i]) {
      if (i0 == -1) {
        i0 = i;
      } else {
        return false;
      }
    }
  }
  if (i0 == -1) {
    return true;
  } else {
    rep(j, w) {
      if (s[i0][idx[j]] != s[i0][idx[w - 1 - j]]) {
        return false;
      }
    }
    return true;
  }
}

// #include <random>

int main() {
  auto start = clock();
  random_device seed_gen;
  mt19937 rand(seed_gen());
  cin >> h >> w;
  s.resize(h);
  idx.resize(w);
  rep(i, h) {
    s[i].resize(w);
    rep(j, w) { cin >> s[i][j]; }
  }

  iota(all(idx), 0);
  while ((clock() - start) / CLOCKS_PER_SEC < 1.9) {
    shuffle(all(idx), rand);
    // DEBUG_VEC(idx);
    if (check()) {
      cout << "YES" << endl;
      return 0;
    }
  }
  cout << "NO" << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define DEBUG(x) cout << #x << ": " << x << endl;
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  for (int i = 0; i < v.size(); i++)                                           \
    cout << " " << v[i];                                                       \
  cout << endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define in(x, a, b) a <= x &&x < b
const int inf = 1000000001;
const ll INF = 2e18;
const ll MOD = 1000000007;
// const ll mod = 1000000009;
const double pi = 3.14159265358979323846;
#define Sp(p) cout << setprecision(15) << fixed << p << endl;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx2[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy2[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int h, w;
vector<vector<char>> s;
vi idx;

bool cmp(int i, int j) {
  rep(k, w) {
    if (s[i][idx[k]] != s[j][idx[w - 1 - k]]) {
      return false;
    }
  }
  return true;
}

bool check() {
  vector<bool> used(h, false);
  rep(i, h) {
    if (used[i]) {
      continue;
    }
    for (int j = i + 1; j < h; j++) {
      if (used[j]) {
        continue;
      }
      if (cmp(i, j)) {
        used[i] = used[j] = true;
        break;
      }
    }
  }
  // DEBUG_VEC(used);
  int i0 = -1;
  rep(i, h) {
    if (!used[i]) {
      if (i0 == -1) {
        i0 = i;
      } else {
        return false;
      }
    }
  }
  if (i0 == -1) {
    return true;
  } else {
    rep(j, w) {
      if (s[i0][idx[j]] != s[i0][idx[w - 1 - j]]) {
        return false;
      }
    }
    return true;
  }
}

// #include <random>

int main() {
  auto start = clock();
  random_device seed_gen;
  mt19937 rand(seed_gen());
  cin >> h >> w;
  s.resize(h);
  idx.resize(w);
  rep(i, h) {
    s[i].resize(w);
    rep(j, w) { cin >> s[i][j]; }
  }

  iota(all(idx), 0);
  while ((clock() - start) < 1.9 * CLOCKS_PER_SEC) {
    shuffle(all(idx), rand);
    // DEBUG_VEC(idx);
    if (check()) {
      cout << "YES" << endl;
      return 0;
    }
  }
  cout << "NO" << endl;
}