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

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
template <typename T> void minUpdate(T &a, T b) { a = min(a, b); }

int violate[10][6] = {
    {-1}, {2, 3, 6, 8, 9, -1}, {3, 6, 9, -1}, {-1}, {2, 3, 6, 8, 9, -1}, {-1},
    {-1}, {2, 3, 6, 8, 9, -1}, {3, 6, 9, -1}, {-1},
};

int dp[2][2][10][10];

bool check(int L, int R) {
  for (int j = 0; violate[R][j] != -1; ++j)
    if (violate[R][j] == L)
      return false;
  return true;
}

int main() {
  string S;

  while (cin >> S, S != "#") {
    int m = S.size();

    for_(b, 0, 2) for_(L, 0, 10) for_(R, 0, 10) dp[0][b][L][R] = 1L << 30;
    dp[0][0][0][S[0] - '0'] = 0;
    dp[0][1][S[0] - '0'][0] = 0;

    for_(i, 1, m) {
      int nLR = S[i] - '0';
      for_(b, 0, 2) for_(L, 0, 10) for_(R, 0, 10) dp[1][b][L][R] = 1L << 30;
      for_(b, 0, 2) for_(L, 0, 10) for_(R, 0, 10) {
        if (dp[0][b][L][R] == 1L << 30)
          continue;
        if (check(nLR, R))
          minUpdate(dp[1][1][nLR][R], dp[0][b][L][R] + (b ? 1 : 0));
        if (check(L, nLR))
          minUpdate(dp[1][0][R][nLR], dp[0][b][L][R] + (b ? 0 : 1));
      }
      swap(dp[0], dp[1]);
    }

    int ans = 1L << 30;
    for_(b, 0, 2) for_(L, 0, 10) for_(R, 0, 10) minUpdate(ans, dp[0][b][L][R]);
    cout << ans << endl;
    /*
    int L[2] = {0, 0}, R[2] = {0, 0}, cnt[2] = {0, 0};
    bool flag[2] = {false, true};

    for_(i,0,m) {
            for_(b,0,2) {
                    int r = (flag[b] ? S[i] - '0' : R[b]), l = (flag[b] ? L[b] :
    S[i] - '0'); bool inv = false; for (int j=0; violate[r][j] != -1; ++j) inv
    |= (violate[r][j] == l);

                    if (inv) {
                            if (flag[b]) L[b] = r;
                            else R[b] = l;
                            ++cnt[b];
                    } else {
                            R[b] = r;
                            L[b] = l;
                            flag[b] = !flag[b];
                    }
            }
    }
    cout << min(cnt[0], cnt[1]) << endl;
    */
  }
}

Error: MLE

Fixed submission:

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
template <typename T> void minUpdate(T &a, T b) { a = min(a, b); }

int violate[10][6] = {
    {},   {2, 3, 6, 8, 9, -1}, {3, 6, 9, -1}, {-1}, {2, 3, 6, 8, 9, -1}, {},
    {-1}, {2, 3, 6, 8, 9, -1}, {3, 6, 9, -1}, {-1},
};

int dp[100005][2][10][10];

int main() {
  string S;

  while (cin >> S, S != "#") {
    int m = S.size();

    for_(i, 0, m + 1) for_(b, 0, 2) for_(L, 0, 10) for_(R, 0, 10)
        dp[i][b][L][R] = 1L << 30;
    dp[0][0][0][0] = 0;
    dp[0][1][0][0] = 0;

    for_(i, 0, m) for_(b, 0, 2) for_(L, 0, 10) for_(R, 0, 10) {
      if (dp[i][b][L][R] == 1L << 30)
        continue;
      int nR = (b ? S[i] - '0' : R), nL = (b ? L : S[i] - '0');
      bool inv = false;
      for (int j = 0; violate[nR][j] != -1; ++j)
        inv |= (violate[nR][j] == nL);

      if (inv)
        minUpdate(dp[i + 1][b][(b ? nR : L)][(b ? R : nL)], dp[i][b][L][R] + 1);
      else
        minUpdate(dp[i + 1][1 - b][nL][nR], dp[i][b][L][R]);
    }

    int ans = 1L << 30;
    for_(b, 0, 2) for_(L, 0, 10) for_(R, 0, 10) minUpdate(ans, dp[m][b][L][R]);
    cout << ans << endl;
    /*
    int L[2] = {0, 0}, R[2] = {0, 0}, cnt[2] = {0, 0};
    bool flag[2] = {false, true};

    for_(i,0,m) {
            for_(b,0,2) {
                    int r = (flag[b] ? S[i] - '0' : R[b]), l = (flag[b] ? L[b] :
    S[i] - '0'); bool inv = false; for (int j=0; violate[r][j] != -1; ++j) inv
    |= (violate[r][j] == l);

                    if (inv) {
                            if (flag[b]) L[b] = r;
                            else R[b] = l;
                            ++cnt[b];
                    } else {
                            R[b] = r;
                            L[b] = l;
                            flag[b] = !flag[b];
                    }
            }
    }
    cout << min(cnt[0], cnt[1]) << endl;
    */
  }
}