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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int col[] = {0, -1, 0, 1, -1, 0, 1, -1, 0, 1};
bool ok(int left, int right) { return col[left] <= col[right]; }
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
int main() {
  string s;
  while (cin >> s, s != "#") {
    int n = s.size();
    vvvvi dp(2, vvvi(10, vvi(10, vi(2, INT_MAX / 10))));
    REP(j, 10) {
      REP(k, 10) {
        REP(l, 2) { dp[0][j][k][l] = 0; }
      }
    }
    REP(i, n) {
      REP(j, 10) {
        REP(k, 10) {
          REP(l, 2) { dp[(i + 1) % 2][j][k][l] = INT_MAX / 10; }
        }
      }
      REP(j, 10) {
        REP(k, 10) {
          int p = s[i] - '0';
          if (ok(p, k)) {
            REP(l, 2) {
              dp[(i + 1) % 2][p][k][0] =
                  min(dp[(i + 1) % 2][p][k][0], dp[i % 2][j][k][l] + (l == 0));
            }
          }
          if (ok(j, p)) {
            REP(l, 2) {
              dp[(i + 1) % 2][j][p][1] =
                  min(dp[(i + 1) % 2][j][p][1], dp[i % 2][j][k][l] + (l == 1));
            }
          }
        }
      }
    }
    int ans = INT_MAX;
    /*
    REP(i,n+1){
      REP(j,10){
        REP(k,10){
          REP(l,2){
            cerr<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<dp[i%2][j][k][l]<<endl;
          }
        }
      }
    }
    */
    REP(j, 10) {
      REP(k, 10) {
        REP(l, 2) { ans = min(ans, dp[n % 2][j][k][l]); }
      }
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int col[] = {0, -1, 0, 1, -1, 0, 1, -1, 0, 1};
bool ok(int left, int right) { return col[left] <= col[right]; }
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
int main() {
  string s;
  while (cin >> s, s != "#") {
    int n = s.size();
    vvvvi dp(n + 1, vvvi(10, vvi(10, vi(2, INT_MAX / 10))));
    REP(j, 10) {
      REP(k, 10) {
        REP(l, 2) { dp[0][j][k][l] = 0; }
      }
    }
    REP(i, n) {
      REP(j, 10) {
        REP(k, 10) {
          int p = s[i] - '0';
          if (ok(p, k)) {
            REP(l, 2) {
              dp[i + 1][p][k][0] =
                  min(dp[i + 1][p][k][0], dp[i][j][k][l] + (l == 0));
            }
          }
          if (ok(j, p)) {
            REP(l, 2) {
              dp[i + 1][j][p][1] =
                  min(dp[i + 1][j][p][1], dp[i][j][k][l] + (l == 1));
            }
          }
        }
      }
    }
    int ans = INT_MAX;
    /*
    REP(i,n+1){
      REP(j,10){
        REP(k,10){
          REP(l,2){
            cerr<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<dp[i][j][k][l]<<endl;
          }
        }
      }
    }
    */
    REP(j, 10) {
      REP(k, 10) {
        REP(l, 2) { ans = min(ans, dp[n][j][k][l]); }
      }
    }
    cout << ans << endl;
  }
}