Problem p03593 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = (n)-1; i >= 0; i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a, 0, sizeof(a))
#define inf (1ll << 60)
#define Yes(f) cout << (f ? "Yes" : "No") << endl
#define yes(f) cout << (f ? "yes" : "no") << endl
#define YES(f) cout << (f ? "YES" : "NO") << endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod = 1e9 + 7;
const string sp = " ";

void run();

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
}

signed main() {
  init();
  run();
  return 0;
}

void run() {
  int h, w;
  cin >> h >> w;
  vector<string> a(h);
  rep(i, h) cin >> a[i];
  vi c(26);
  rep(i, h) rep(j, w) c[a[i][j] - 'a']++;
  int x1 = (h % 2) * (w % 2);
  int x3 = (h / 2) * (w / 2);
  int x2 = (h * w - x1 - x3 * 4) / 2;
  rep(_, x1) {
    rep(i, 26) {
      if (c[i] % 2) {
        c[i]--;
        break;
      }
    }
  }
  rep(_, x2) {
    bool f = false;
    bool g = false;
  L:;
    int i;
    for (i = 0; i < 26; i++) {
      if (g && c[i] && c[i] % 2 == 0) {
        c[i] -= 2;
        f = false;
        g = false;
        break;
      }
      if (c[i] % 4 == 2) {
        c[i] -= 2;
        f = false;
        g = false;
        break;
      }
      if (c[i] % 4 == 0)
        f = true;
    }
    if (f) {
      g = true;
      goto L;
    }
  }
  rep(_, x3) {
    rep(i, 26) {
      if (c[i] > 0 && c[i] % 4 == 0) {
        c[i] -= 4;
        break;
      }
    }
  }
  bool f = true;
  rep(i, 26) { f &= c[i] == 0; }
  Yes(f);
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = (n)-1; i >= 0; i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a, 0, sizeof(a))
#define inf (1ll << 60)
#define Yes(f) cout << (f ? "Yes" : "No") << endl
#define yes(f) cout << (f ? "yes" : "no") << endl
#define YES(f) cout << (f ? "YES" : "NO") << endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod = 1e9 + 7;
const string sp = " ";

void run();

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
}

signed main() {
  init();
  run();
  return 0;
}

void run() {
  int h, w;
  cin >> h >> w;
  vector<string> a(h);
  rep(i, h) cin >> a[i];
  vi c(26);
  rep(i, h) rep(j, w) c[a[i][j] - 'a']++;
  int x1 = (h % 2) * (w % 2);
  int x3 = (h / 2) * (w / 2);
  int x2 = (h * w - x1 - x3 * 4) / 2;
  rep(_, x1) {
    rep(i, 26) {
      if (c[i] % 2) {
        c[i]--;
        break;
      }
    }
  }
  rep(_, x2) {
    bool f = false;
    bool g = false;
  L:;
    int i;
    for (i = 0; i < 26; i++) {
      if (g && c[i] && c[i] % 2 == 0) {
        c[i] -= 2;
        f = false;
        g = false;
        break;
      }
      if (c[i] % 4 == 2) {
        c[i] -= 2;
        f = false;
        g = false;
        break;
      }
      if (c[i] && c[i] % 4 == 0)
        f = true;
    }
    if (f) {
      g = true;
      goto L;
    }
  }
  rep(_, x3) {
    rep(i, 26) {
      if (c[i] > 0 && c[i] % 4 == 0) {
        c[i] -= 4;
        break;
      }
    }
  }
  bool f = true;
  rep(i, 26) { f &= c[i] == 0; }
  Yes(f);
}