Problem p00482 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600
#define MOD 100000

int dpdp[2][1 << 20][2];

int main() {
  int m, n;
  cin >> m >> n;
  vector<string> vec(m);
  rep(i, m) cin >> vec[i];
  // ??????m,??????n

  auto dp = dpdp[0];
  fill(dp[0], dp[1 << n], 0);
  auto nxt = dpdp[1];
  dp[0][0] = 1;

  rep(i, m) rep(j, n) {
    fill(nxt[0], nxt[1 << n], 0);
    rep(mask, 1 << n) rep(b, 2) if (dp[mask][b] > 0) {
      dp[mask][b] %= MOD;
      if (vec[i][j] == '?' || vec[i][j] == 'J') {
        int nmask = mask | (1 << j);
        if (j > 0)
          nmask &= ~(1 << (j - 1));
        nxt[nmask][b] += dp[mask][b];
      }
      if (vec[i][j] == '?' || vec[i][j] == 'O') {
        int nmask = mask & ~(1 << j);
        nxt[nmask][b] += dp[mask][b];
      }
      if (vec[i][j] == '?' || vec[i][j] == 'I') {
        int nmask = mask & ~(1 << j);
        if (j > 0)
          nmask &= ~(1 << (j - 1));
        int nb = b;
        if (j < n - 1 && (mask & (1 << j)))
          nb = 1;
        nxt[nmask][nb] += dp[mask][b];
      }
    }
    swap(nxt, dp);
  }

  int ans = 0;
  rep(i, 1 << n)(ans += dp[i][1]) %= MOD;
  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600
#define MOD 100000

int main() {
  int m, n;
  cin >> m >> n;
  vector<string> vec(m);
  rep(i, m) cin >> vec[i];
  // ??????m,??????n

  vector<vector<int>> dp(1 << n, vector<int>(2, 0));
  dp[0][0] = 1;

  rep(i, m) rep(j, n) {
    vector<vector<int>> nxt(1 << n, vector<int>(2, 0));
    rep(mask, 1 << n) rep(b, 2) if (dp[mask][b] > 0) {
      dp[mask][b] %= MOD;
      if (vec[i][j] == '?' || vec[i][j] == 'J') {
        int nmask = mask | (1 << j);
        if (j > 0)
          nmask &= ~(1 << (j - 1));
        nxt[nmask][b] += dp[mask][b];
      }
      if (vec[i][j] == '?' || vec[i][j] == 'O') {
        int nmask = mask & ~(1 << j);
        nxt[nmask][b] += dp[mask][b];
      }
      if (vec[i][j] == '?' || vec[i][j] == 'I') {
        int nmask = mask & ~(1 << j);
        if (j > 0)
          nmask &= ~(1 << (j - 1));
        int nb = b;
        if (j < n - 1 && (mask & (1 << j)))
          nb = 1;
        nxt[nmask][nb] += dp[mask][b];
      }
    }
    swap(nxt, dp);
  }

  int ans = 0;
  rep(i, 1 << n)(ans += dp[i][1]) %= MOD;
  cout << ans << endl;

  return 0;
}