Problem p03383 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  REP(i, h) cin >> s[i];

  vector<string> x(h), y(w);
  REP(i, h) REP(j, w) {
    x[i] += s[i][j];
    y[j] += s[i][j];
  }

  REP(i, h) sort(ALL(x[i]));
  REP(i, w) sort(ALL(y[i]));

  VI p(h, -1), q(w, -1);

  map<string, VI> mp;
  REP(i, h) { mp[x[i]].push_back(i); }

  int j = 0;
  for (auto a : mp) {
    VI v = a.second;
    REP(i, v.size() / 2) {
      p[j] = v[2 * i];
      p[h - 1 - j] = v[2 * i + 1];
      j++;
    }
    if (v.size() % 2 == 1) {
      if (h % 2 == 0 || p[h / 2] != -1) {
        cout << "NO" << endl;
        return 0;
      }
      p[h / 2] = v.back();
    }
  }

  mp.clear();
  REP(i, w) { mp[y[i]].push_back(i); }

  j = 0;
  for (auto a : mp) {
    VI v = a.second;
    REP(i, v.size() / 2) {
      q[j] = v[2 * i];
      q[w - 1 - j] = v[2 * i + 1];
      j++;
    }
    if (v.size() % 2 == 1) {
      if (w % 2 == 0 || q[w / 2] != -1) {
        cout << "NO" << endl;
        return 0;
      }
      q[h / 2] = v.back();
    }
  }

  assert(*min_element(ALL(p)) == 0);
  assert(*min_element(ALL(q)) == 0);

  // for (int x : p) cout << x << " ";
  // cout << endl;
  // for (int x : q) cout << x << " ";
  // cout << endl;

  int hh = h / 2;
  int ww = w / 2;

  VI pp(hh);
  REP(i, hh) pp[i] = p[i];
  VI qq(ww);
  REP(i, ww) qq[i] = q[i];

  sort(ALL(pp));
  sort(ALL(qq));

  do {
    REP(i, hh) p[i] = pp[i];
    // for (int x : p) cout << x << " ";
    // cout << endl;
    do {
      REP(i, ww) q[i] = qq[i];
      bool ok = true;
      REP(i, h) REP(j, w) {
        if (s[p[i]][q[j]] != s[p[h - 1 - i]][q[w - 1 - j]]) {
          ok = false;
        }
      }
      if (ok) {
        cout << "YES" << endl;
        return 0;
      }
    } while (next_permutation(ALL(qq)));
  } while (next_permutation(ALL(pp)));

  // REP(i,h) REP(j,w){
  //     if (s[p[i]][q[j]] != s[p[h-1-i]][q[w-1-j]]){
  //         cout << "NO" << endl;
  //         return 0;
  //     }
  // }

  // cout << "YES" << endl;

  cout << "NO" << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  REP(i, h) cin >> s[i];

  vector<string> x(h), y(w);
  REP(i, h) REP(j, w) {
    x[i] += s[i][j];
    y[j] += s[i][j];
  }

  REP(i, h) sort(ALL(x[i]));
  REP(i, w) sort(ALL(y[i]));

  VI p(h, -1), q(w, -1);

  map<string, VI> mp;
  REP(i, h) { mp[x[i]].push_back(i); }

  int j = 0;
  for (auto a : mp) {
    VI v = a.second;
    REP(i, v.size() / 2) {
      p[j] = v[2 * i];
      p[h - 1 - j] = v[2 * i + 1];
      j++;
    }
    if (v.size() % 2 == 1) {
      if (h % 2 == 0 || p[h / 2] != -1) {
        cout << "NO" << endl;
        return 0;
      }
      p[h / 2] = v.back();
    }
  }

  mp.clear();
  REP(i, w) { mp[y[i]].push_back(i); }

  j = 0;
  for (auto a : mp) {
    VI v = a.second;
    REP(i, v.size() / 2) {
      q[j] = v[2 * i];
      q[w - 1 - j] = v[2 * i + 1];
      j++;
    }
    if (v.size() % 2 == 1) {
      if (w % 2 == 0 || q[w / 2] != -1) {
        cout << "NO" << endl;
        return 0;
      }
      q[w / 2] = v.back();
    }
  }

  assert(*min_element(ALL(p)) == 0);
  assert(*min_element(ALL(q)) == 0);

  // for (int x : p) cout << x << " ";
  // cout << endl;
  // for (int x : q) cout << x << " ";
  // cout << endl;

  int hh = h / 2;
  int ww = w / 2;

  VI pp(hh);
  REP(i, hh) pp[i] = p[i];
  VI qq(ww);
  REP(i, ww) qq[i] = q[i];

  sort(ALL(pp));
  sort(ALL(qq));

  do {
    REP(i, hh) p[i] = pp[i];
    // for (int x : p) cout << x << " ";
    // cout << endl;
    do {
      REP(i, ww) q[i] = qq[i];
      bool ok = true;
      REP(i, h) REP(j, w) {
        if (s[p[i]][q[j]] != s[p[h - 1 - i]][q[w - 1 - j]]) {
          ok = false;
        }
      }
      if (ok) {
        cout << "YES" << endl;
        return 0;
      }
    } while (next_permutation(ALL(qq)));
  } while (next_permutation(ALL(pp)));

  // REP(i,h) REP(j,w){
  //     if (s[p[i]][q[j]] != s[p[h-1-i]][q[w-1-j]]){
  //         cout << "NO" << endl;
  //         return 0;
  //     }
  // }

  // cout << "YES" << endl;

  cout << "NO" << endl;

  return 0;
}