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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
        for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
                if (i) os << " ";
                os << get<0>(t);
        }
        return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

int dp[2][9][9][2];
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  string s;
  const int X[] = {0, 1, 2, 0, -1, 2, 0, 1, 2};
  while (cin >> s, s != "#") {
    int N = s.size();
    vector<int> v(N);
    REP(i, N) v[i] = s[i] - '1';
    fill(dp[0][0][0], dp[1][8][9], inf);
    dp[0][4][4][0] = dp[0][4][4][1] = 0;
    REP(i, N) {
      fill(dp[i + 1 & 1][0][0], dp[i + 1 & 1][8][9], inf);
      REP(j, 9) REP(k, 9) REP(p, 2) {
        if (dp[i & 1][j][k][p] == inf)
          continue;
        //				cout << i << " " << j << " " << k << " " << p
        //<< " " << dp[i][j][k][p] << endl;
        REP(l, 2) {
          int nj = l == 0 ? j : v[i];
          int nk = l == 1 ? k : v[i];
          if (nj != 4 && nk != 4 && X[nj] > X[nk])
            continue;
          if (i > 0 && (nj == 4 || nk == 4))
            continue;
          int cost = p == l ? 1 : 0;
          //					cout << nj << " " << nk << " " << l <<
          //endl;
          dp[i + 1 & 1][nj][nk][l] =
              min(dp[i + 1 & 1][nj][nk][l], dp[i & 1][j][k][p] + cost);
        }
      }
    }
    int ans = inf;
    REP(i, 9) REP(j, 9) REP(k, 2) { ans = min(ans, dp[N & 1][i][j][k]); }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
        for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
                if (i) os << " ";
                os << get<0>(t);
        }
        return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

int dp[100001][9][9][2];
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  string s;
  const int X[] = {0, 1, 2, 0, -1, 2, 0, 1, 2};
  while (cin >> s, s != "#") {
    int N = s.size();
    vector<int> v(N);
    REP(i, N) v[i] = s[i] - '1';
    fill(dp[0][0][0], dp[N][8][9], inf);
    dp[0][4][4][0] = dp[0][4][4][1] = 0;
    REP(i, N) REP(j, 9) REP(k, 9) REP(p, 2) {
      if (dp[i][j][k][p] == inf)
        continue;
      //			cout << i << " " << j << " " << k << " " << p <<
      //" " << dp[i][j][k][p] << endl;
      REP(l, 2) {
        int nj = l == 0 ? j : v[i];
        int nk = l == 1 ? k : v[i];
        if (nj != 4 && nk != 4 && X[nj] > X[nk])
          continue;
        if (i > 0 && (nj == 4 || nk == 4))
          continue;
        int cost = p == l ? 1 : 0;
        //				cout << nj << " " << nk << " " << l <<
        //endl;
        dp[i + 1][nj][nk][l] = min(dp[i + 1][nj][nk][l], dp[i][j][k][p] + cost);
      }
    }
    int ans = inf;
    REP(i, 9) REP(j, 9) REP(k, 2) { ans = min(ans, dp[N][i][j][k]); }
    cout << ans << endl;
  }
}