Problem p03593 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define FORE(i, a) for (auto i = a.begin(); i != a.end(); ++i)
#define REPU(i, a, b) for (int i = (a); i < (b); ++i)
#define REPD(i, a, b) for (int i = (a); i > (b); --i)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())

vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c))
    v.push_back(x);
  return v;
}
#define DEBUG(args...)                                                         \
  {                                                                            \
    vector<string> _v = split(#args, ',');                                     \
    err(_v.begin(), args);                                                     \
  }
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cerr << "[DEBUG] " << it->substr((*it)[0] == ' ', it->length()) << " = " << a
       << '\n';
  err(++it, args...);
}

typedef long long ll;
const int MOD = 1000000007;

template <class T, class U> inline T tmin(T a, U b) { return (a < b) ? a : b; }
template <class T, class U> inline T tmax(T a, U b) { return (a > b) ? a : b; }
template <class T, class U> inline void amax(T &a, U b) {
  if (b > a)
    a = b;
}
template <class T, class U> inline void amin(T &a, U b) {
  if (b < a)
    a = b;
}
template <class T> T gcd(T a, T b) {
  while (b != 0) {
    T c = a;
    a = b;
    b = c % b;
  }
  return a;
}

char s[101][101];
int need[10];

void calc(int n, int m) {
  if (n > m)
    swap(n, m);
  if (n == 1) {
    need[2] += m / 2, need[1] += m & 1;
    return;
  }
  need[4] += n / 2;
  if (n & 1)
    need[2]++;
  need[4] += (m - 2) / 2;
  if (m & 1)
    need[2]++;
  calc(n - 2, m - 2);
  return;
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  REPU(i, 0, n) cin >> s[i];
  vector<int> cnt(26, 0);
  REPU(i, 0, n) REPU(j, 0, m) { cnt[s[i][j] - 'a']++; }
  calc(n, m);
  int tot = 0;
  REPU(i, 0, 26) {
    tot += cnt[i] / 4;
    cnt[i] &= 3;
  }
  bool ok(true);
  if (tot >= need[4])
    tot -= need[4], tot *= 2;
  else
    ok = false;
  REPU(i, 0, 26) { tot += cnt[i] / 2; }
  if (tot < need[2])
    ok = false;
  puts(ok ? "Yes" : "No");

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define FORE(i, a) for (auto i = a.begin(); i != a.end(); ++i)
#define REPU(i, a, b) for (int i = (a); i < (b); ++i)
#define REPD(i, a, b) for (int i = (a); i > (b); --i)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())

vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c))
    v.push_back(x);
  return v;
}
#define DEBUG(args...)                                                         \
  {                                                                            \
    vector<string> _v = split(#args, ',');                                     \
    err(_v.begin(), args);                                                     \
  }
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cerr << "[DEBUG] " << it->substr((*it)[0] == ' ', it->length()) << " = " << a
       << '\n';
  err(++it, args...);
}

typedef long long ll;
const int MOD = 1000000007;

template <class T, class U> inline T tmin(T a, U b) { return (a < b) ? a : b; }
template <class T, class U> inline T tmax(T a, U b) { return (a > b) ? a : b; }
template <class T, class U> inline void amax(T &a, U b) {
  if (b > a)
    a = b;
}
template <class T, class U> inline void amin(T &a, U b) {
  if (b < a)
    a = b;
}
template <class T> T gcd(T a, T b) {
  while (b != 0) {
    T c = a;
    a = b;
    b = c % b;
  }
  return a;
}

char s[101][101];
int need[10];

void calc(int n, int m) {
  if (n > m)
    swap(n, m);
  if (n == 0)
    return;
  if (n == 1) {
    need[2] += m / 2, need[1] += m & 1;
    return;
  }
  need[4] += n / 2;
  if (n & 1)
    need[2]++;
  need[4] += (m - 2) / 2;
  if (m & 1)
    need[2]++;
  calc(n - 2, m - 2);
  return;
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  REPU(i, 0, n) cin >> s[i];
  vector<int> cnt(26, 0);
  REPU(i, 0, n) REPU(j, 0, m) { cnt[s[i][j] - 'a']++; }
  calc(n, m);
  int tot = 0;
  REPU(i, 0, 26) {
    tot += cnt[i] / 4;
    cnt[i] &= 3;
  }
  bool ok(true);
  if (tot >= need[4])
    tot -= need[4], tot *= 2;
  else
    ok = false;
  REPU(i, 0, 26) { tot += cnt[i] / 2; }
  if (tot < need[2])
    ok = false;
  puts(ok ? "Yes" : "No");

  return 0;
}