Problem p03383 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

int check(string s, string t) {
  vector<bool> used(s.size(), false);

  rep(i, s.size()) {
    if (used[i])
      continue;
    char sc = s[i];
    char tc = t[i];
    rep(j, s.size()) {
      if (i == j)
        continue;
      if (used[j])
        continue;
      char sb = s[j];
      char tb = t[j];
      if (sc == tb && tc == sb) {
        used[i] = true;
        used[j] = true;
        break;
      }
    }
  }
  int cnt = 0;
  rep(i, s.size()) {
    if (used[i]) {
      cnt++;
    }
  }
  return cnt;
}

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> v;
  rep(i, H) {
    string s;
    cin >> s;
    v.push_back(s);
  }

  vector<vector<int>> w;
  rep(i, H) {
    vector<int> tmp;
    rep(j, H) {
      if (i == j)
        continue;
      int res = check(v[i], v[j]);
      if ((W % 2 == 0 && res == W) || (W % 2 == 1 && res == W - 1)) {
        tmp.push_back(j);
      }
    }
    w.push_back(tmp);
  }

  vector<bool> used(H, false);
  vector<pair<int, int>> pr;
  rep(i, H) {
    if (used[i])
      continue;
    rep(j, w[i].size()) {
      if (used[w[i][j]])
        continue;
      pr.push_back(make_pair(i, w[i][j]));
      used[i] = true;
      used[w[i][j]] = true;
      break;
    }
  }
  int cnt = 0;
  rep(i, H) {
    if (used[i])
      cnt++;
  }

  if ((H % 2 == 0 && cnt != H) || (H % 2 == 1 && cnt != H - 1)) {
    cout << "NO" << endl;
    return 0;
  }

  vector<bool> used2(W, false);
  int base1 = pr[0].first;
  int base2 = pr[0].second;
  rep(j, W) {
    if (used2[j])
      continue;

    REP(k, j + 1, W) {
      if (v[base1][j] == v[base2][k] && v[base2][j] == v[base1][k]) {
        bool flg = true;
        REP(l, 1, pr.size()) {
          int tr1 = pr[l].first;
          int tr2 = pr[l].second;
          if (v[tr1][j] == v[tr2][k] && v[tr2][j] == v[tr1][k]) {
            ; // ok
          } else {
            flg = false;
          }
        }

        if (flg) {
          used2[j] = true;
          used2[k] = true;
          break;
        }
      }
    }
  }
  cnt = 0;
  rep(i, W) {
    if (used2[i])
      cnt++;
  }

  if ((W % 2 == 0 && cnt != W) || (W % 2 == 1 && cnt != W - 1)) {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

int check(string s, string t) {
  vector<bool> used(s.size(), false);

  rep(i, s.size()) {
    if (used[i])
      continue;
    char sc = s[i];
    char tc = t[i];
    rep(j, s.size()) {
      if (i == j)
        continue;
      if (used[j])
        continue;
      char sb = s[j];
      char tb = t[j];
      if (sc == tb && tc == sb) {
        used[i] = true;
        used[j] = true;
        break;
      }
    }
  }
  int cnt = 0;
  rep(i, s.size()) {
    if (used[i]) {
      cnt++;
    }
  }
  return cnt;
}

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> v;
  rep(i, H) {
    string s;
    cin >> s;
    v.push_back(s);
  }

  vector<vector<int>> w;
  rep(i, H) {
    vector<int> tmp;
    rep(j, H) {
      if (i == j)
        continue;
      int res = check(v[i], v[j]);
      if ((W % 2 == 0 && res == W) || (W % 2 == 1 && res == W - 1)) {
        tmp.push_back(j);
      }
    }
    w.push_back(tmp);
  }

  vector<bool> used(H, false);
  vector<pair<int, int>> pr;
  rep(i, H) {
    if (used[i])
      continue;
    rep(j, w[i].size()) {
      if (used[w[i][j]])
        continue;
      pr.push_back(make_pair(i, w[i][j]));
      used[i] = true;
      used[w[i][j]] = true;
      break;
    }
  }
  int cnt = 0;
  rep(i, H) {
    if (used[i])
      cnt++;
  }

  if ((H % 2 == 0 && cnt != H) || (H % 2 == 1 && cnt != H - 1)) {
    cout << "NO" << endl;
    return 0;
  }
  if (pr.size() == 0) {
    cout << "YES" << endl;
    return 0;
  }

  vector<bool> used2(W, false);
  int base1 = pr[0].first;
  int base2 = pr[0].second;
  rep(j, W) {
    if (used2[j])
      continue;

    REP(k, j + 1, W) {
      if (v[base1][j] == v[base2][k] && v[base2][j] == v[base1][k]) {
        bool flg = true;
        REP(l, 1, pr.size()) {
          int tr1 = pr[l].first;
          int tr2 = pr[l].second;
          if (v[tr1][j] == v[tr2][k] && v[tr2][j] == v[tr1][k]) {
            ; // ok
          } else {
            flg = false;
          }
        }

        if (flg) {
          used2[j] = true;
          used2[k] = true;
          break;
        }
      }
    }
  }
  cnt = 0;
  rep(i, W) {
    if (used2[i])
      cnt++;
  }

  if ((W % 2 == 0 && cnt != W) || (W % 2 == 1 && cnt != W - 1)) {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;

  return 0;
}