Problem p03383 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...) 0
#endif

inline bool ts(string x) {
  vector<int> a(26);
  rep(i, 0, x.size()) a[x[i] - 'a']++;
  if (x.size() % 2) {
    int cnt = 0;
    rep(i, 0, 26) if (a[i] % 2) {
      if (cnt)
        return false;
      cnt++;
    }
  } else {
    rep(i, 0, 26) if (a[i] % 2) return false;
  }
  return true;
}

inline bool ok(string x, string y) {
  int n = x.size(), cnt = 0;
  if (n % 2 == 0)
    cnt++;
  rep(i, 0, n) {
    if (x[i] == '.')
      continue;
    rep(j, i + 1, n) {
      if (i == j || x[j] == '.')
        continue;
      if (x[i] == y[j] && x[j] == y[i]) {
        x[i] = y[i] = x[j] = y[j] = '.';
        break;
      }
    }
    if (x[i] != '.') {
      if (cnt == 0 && x[i] == y[i]) {
        x[i] = y[i] = '.';
        cnt++;
        continue;
      }
      return false;
    }
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  if (h == 1 || w == 1) {
    string s;
    rep(i, 0, h) {
      string t;
      cin >> t;
      s += t;
    }
    if (ts(s))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
    return 0;
  }
  vector<string> s(h), t(h);
  rep(i, 0, h) cin >> s[i];
  string ans = "YES";
  int mx = 0, cnt = 0;
  if (h % 2 == 0)
    cnt++;
  rep(i, 0, h) {
    if (s[i].empty())
      continue;
    bool pass = false;
    rep(j, i + 1, h) {
      if (s[j].empty())
        continue;
      if (ok(s[i], s[j])) {
        t[mx] = s[i];
        t[h - 1 - mx] = s[j];
        s[i] = s[j] = "";
        mx++;
        pass = true;
        break;
      }
    }
    if (!pass && cnt == 0 && ts(s[i])) {
      t[(h - 1) / 2] = s[i];
      s[i] = "";
      cnt++;
      pass = true;
    }
    if (!pass) {
      ans = "NO";
    }
  }
  debug(t);
  vector<string> u(w), v(w);
  rep(i, 0, h) rep(j, 0, w) { u[j] += t[i][j]; }
  // rep(i, 0, w) cout << u[i] << endl;
  mx = 0, cnt = 0;
  if (w % 2 == 0)
    cnt++;
  rep(i, 0, w) {
    if (u[i].empty())
      continue;
    bool pass = false;
    rep(j, i + 1, w) {
      if (u[j].empty())
        continue;
      if (ok(u[i], u[j])) {
        v[mx] = u[i];
        v[w - 1 - mx] = u[j];
        u[i] = u[j] = "";
        mx++;
        pass = true;
        break;
      }
    }
    if (!pass && cnt == 0 && ts(u[i])) {
      v[(w - 1) / 2] = u[i];
      u[i] = "";
      cnt++;
      pass = true;
    }
    if (!pass) {
      ans = "NO";
    }
  }
  debug(v);
  cout << ans << endl;
  /* rep(j, 0, w) {
      rep(i, 0, h) t[j] += s[i][j];
  }
  int p = 0;
  int mid = 0;
  rep(i, 0, h) rep(j, 0, w) s[i][j] = '.';
  debug(string(h, '.'));
  rep(i, 0, w) {
      if (t[i] == ".") continue;
      rep(j, i + 1, w) {
          if (t[j] == ".") continue;
          string x = t[i], y = t[j];
          int cnt = 0;
          rep(k, 0, h) {
              if (x[k] == '.') continue;
              rep(l, 0, h) {
                  if (l == k) continue;
                  if (x[k] == y[l] && x[l] == y[k] && x[l] != '.') {
                      x[k] = y[l] = x[l] = y[k] = '.';
                      break;
                  }
              }
              if (x[k] != '.' && x[k] == y[k] && cnt == 0 && w % 2 == 1) {
                  cnt++;
                  x[k] = y[k] = '.';
              }
              if (x[k] != '.') break;
          }
          if (x == string(h, '.')) {
              rep(k, 0, h) {
                  s[p][k] = t[i][k];
                  s[w - 1 - p][k] = t[j][k];
              }
              t[i] = ".";
              t[j] = ".";
              p++;
              break;
          } else if (w % 2 == 1 && mid == 0 && ok(t[i])) {
              mid++;
              rep(k, 0, h) {
                  s[(w + 1) / 2][k] = t[i][k];
              }
          }
      }
  }
  debug(s);*/
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...) 0
#endif

inline bool ts(string x) {
  vector<int> a(26);
  rep(i, 0, x.size()) a[x[i] - 'a']++;
  if (x.size() % 2) {
    int cnt = 0;
    rep(i, 0, 26) if (a[i] % 2) {
      if (cnt)
        return false;
      cnt++;
    }
  } else {
    rep(i, 0, 26) if (a[i] % 2) return false;
  }
  return true;
}

inline bool ok(string x, string y) {
  int n = x.size(), cnt = 0;
  if (n % 2 == 0)
    cnt++;
  rep(i, 0, n) {
    if (x[i] == '.')
      continue;
    rep(j, i + 1, n) {
      if (i == j || x[j] == '.')
        continue;
      if (x[i] == y[j] && x[j] == y[i]) {
        x[i] = y[i] = x[j] = y[j] = '.';
        break;
      }
    }
    if (x[i] != '.') {
      if (cnt == 0 && x[i] == y[i]) {
        x[i] = y[i] = '.';
        cnt++;
        continue;
      }
      return false;
    }
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  if (h == 1 || w == 1) {
    string s;
    rep(i, 0, h) {
      string t;
      cin >> t;
      s += t;
    }
    if (ts(s))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
    return 0;
  }
  vector<string> s(h), t(h);
  rep(i, 0, h) cin >> s[i];
  string ans = "YES";
  int mx = 0, cnt = 0;
  if (h % 2 == 0)
    cnt++;
  rep(i, 0, h) {
    if (s[i].empty())
      continue;
    bool pass = false;
    rep(j, i + 1, h) {
      if (s[j].empty())
        continue;
      if (ok(s[i], s[j])) {
        t[mx] = s[i];
        t[h - 1 - mx] = s[j];
        s[i] = s[j] = "";
        mx++;
        pass = true;
        break;
      }
    }
    if (!pass && cnt == 0 && ts(s[i])) {
      t[(h - 1) / 2] = s[i];
      s[i] = "";
      cnt++;
      pass = true;
    }
    if (!pass) {
      ans = "NO";
    }
  }
  debug(t);
  vector<string> u(w), v(w);
  if (ans == "YES")
    rep(i, 0, h) rep(j, 0, w) { u[j] += t[i][j]; }
  // rep(i, 0, w) cout << u[i] << endl;
  mx = 0, cnt = 0;
  if (w % 2 == 0)
    cnt++;
  rep(i, 0, w) {
    if (u[i].empty())
      continue;
    bool pass = false;
    rep(j, i + 1, w) {
      if (u[j].empty())
        continue;
      if (ok(u[i], u[j])) {
        v[mx] = u[i];
        v[w - 1 - mx] = u[j];
        u[i] = u[j] = "";
        mx++;
        pass = true;
        break;
      }
    }
    if (!pass && cnt == 0 && ts(u[i])) {
      v[(w - 1) / 2] = u[i];
      u[i] = "";
      cnt++;
      pass = true;
    }
    if (!pass) {
      ans = "NO";
    }
  }
  debug(v);
  cout << ans << endl;
  /* rep(j, 0, w) {
      rep(i, 0, h) t[j] += s[i][j];
  }
  int p = 0;
  int mid = 0;
  rep(i, 0, h) rep(j, 0, w) s[i][j] = '.';
  debug(string(h, '.'));
  rep(i, 0, w) {
      if (t[i] == ".") continue;
      rep(j, i + 1, w) {
          if (t[j] == ".") continue;
          string x = t[i], y = t[j];
          int cnt = 0;
          rep(k, 0, h) {
              if (x[k] == '.') continue;
              rep(l, 0, h) {
                  if (l == k) continue;
                  if (x[k] == y[l] && x[l] == y[k] && x[l] != '.') {
                      x[k] = y[l] = x[l] = y[k] = '.';
                      break;
                  }
              }
              if (x[k] != '.' && x[k] == y[k] && cnt == 0 && w % 2 == 1) {
                  cnt++;
                  x[k] = y[k] = '.';
              }
              if (x[k] != '.') break;
          }
          if (x == string(h, '.')) {
              rep(k, 0, h) {
                  s[p][k] = t[i][k];
                  s[w - 1 - p][k] = t[j][k];
              }
              t[i] = ".";
              t[j] = ".";
              p++;
              break;
          } else if (w % 2 == 1 && mid == 0 && ok(t[i])) {
              mid++;
              rep(k, 0, h) {
                  s[(w + 1) / 2][k] = t[i][k];
              }
          }
      }
  }
  debug(s);*/
  return 0;
}