Problem p03383 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 15
#define M 5010
using namespace std;
ll n, m;
bool dp[M], rc[N][N];
string s[N];
vector<ll> seq;
void mkrand() {
  vector<ll> v;
  ll i;
  for (i = 0; i < n; i++) {
    v.push_back(i);
  }
  seq.clear();
  while (!v.empty()) {
    i = (rand() + (rand() % 7 + 1) * rand() - rand() % 2 + 172) % v.size();
    seq.push_back(v[i]);
    v.erase(v.begin() + i);
  }
  return;
}
int main() {
  ll i, j, k, mask, tran;
  // srand(time(NULL));
  random_device rd;
  mt19937 mt(rd());
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  tran = 1000000;
  while (tran--) {
    // mkrand();
    memset(rc, true, sizeof(rc));
    memset(dp, false, sizeof(dp));
    for (i = 0; i < m; i++) {
      for (j = 0; j < m; j++) {
        for (k = 0; k < n; k++) {
          if (s[k][i] != s[n - k - 1][j]) {
            rc[i][j] = false;
          }
        }
      }
    }
    dp[0] = true;
    for (mask = 0; mask < (1 << m); mask++) {
      if (!dp[mask]) {
        continue;
      }
      for (i = 0; i < m; i++) {
        for (j = i + 1; j < m; j++) {
          if ((mask & (1 << i)) || (mask & (1 << j))) {
            continue;
          }
          if (rc[i][j]) {
            dp[mask | (1 << i) | (1 << j)] = true;
          }
        }
      }
    }
    mask--;
    if (m % 2 == 0) {
      if (dp[mask]) {
        puts("YES");
        return 0;
      }
    } else {
      for (i = 0; i < m; i++) {
        if (dp[mask ^ (1 << i)] && rc[i][i]) {
          puts("YES");
          return 0;
        }
      }
    }
    shuffle(s, s + n, mt);
  }
  puts("NO");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 15
#define M 5010
using namespace std;
ll n, m;
bool dp[M], rc[N][N];
string s[N];
vector<ll> seq;
void mkrand() {
  vector<ll> v;
  ll i;
  for (i = 0; i < n; i++) {
    v.push_back(i);
  }
  seq.clear();
  while (!v.empty()) {
    i = (rand() + (rand() % 7 + 1) * rand() - rand() % 2 + 172) % v.size();
    seq.push_back(v[i]);
    v.erase(v.begin() + i);
  }
  return;
}
int main() {
  ll i, j, k, mask, tran;
  // srand(time(NULL));
  random_device rd;
  mt19937 mt(rd());
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  tran = 100000;
  while (tran--) {
    // mkrand();
    memset(rc, true, sizeof(rc));
    memset(dp, false, sizeof(dp));
    for (i = 0; i < m; i++) {
      for (j = 0; j < m; j++) {
        for (k = 0; k < n; k++) {
          if (s[k][i] != s[n - k - 1][j]) {
            rc[i][j] = false;
          }
        }
      }
    }
    dp[0] = true;
    for (mask = 0; mask < (1 << m); mask++) {
      if (!dp[mask]) {
        continue;
      }
      for (i = 0; i < m; i++) {
        for (j = i + 1; j < m; j++) {
          if ((mask & (1 << i)) || (mask & (1 << j))) {
            continue;
          }
          if (rc[i][j]) {
            dp[mask | (1 << i) | (1 << j)] = true;
          }
        }
      }
    }
    mask--;
    if (m % 2 == 0) {
      if (dp[mask]) {
        puts("YES");
        return 0;
      }
    } else {
      for (i = 0; i < m; i++) {
        if (dp[mask ^ (1 << i)] && rc[i][i]) {
          puts("YES");
          return 0;
        }
      }
    }
    shuffle(s, s + n, mt);
  }
  puts("NO");
  return 0;
}