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

#include <bits/stdc++.h>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))

using namespace std;

typedef pair<int, int> P;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second

#define INF 1000000000
#define index(a, b, c) (a) * 81 + (b)*9 + (c)

int N;
char s[100004];
int dp[2][2 * 9 * 9];

bool input() {
  cin >> s;
  if (s[0] == '#')
    return false;
  N = strlen(s);
  REP(i, N) s[i] -= '1';
  return true;
}

int x[9] = {0, 1, 2, 0, 1, 2, 0, 1, 2};

void solve() {
  REP(i, 2) {
    REP(j, 9) {
      REP(k, 9) {
        if (x[j] > x[k])
          dp[0][index(i, j, k)] = INF;
        else
          dp[0][index(i, j, k)] = 0;
      }
    }
  }

  REP(l, N) {
    REP(i, 2 * 9 * 9) dp[1][i] = INF;

    REP(i, 2) {
      REP(j, 9) {
        REP(k, 9) {
          if (dp[0][index(i, j, k)] == INF)
            continue;
          if (x[s[l]] <= x[k])
            dp[1][index(0, s[l], k)] =
                min(dp[1][index(0, s[l], k)], dp[0][index(i, j, k)] + (i == 0));
          if (x[s[l]] >= x[j])
            dp[1][index(1, j, s[l])] =
                min(dp[1][index(1, j, s[l])], dp[0][index(i, j, k)] + (i == 1));
        }
      }
    }

    REP(i, 2 * 9 * 9) dp[0][i] = dp[1][i];
  }

  int a = INF;
  /*REP(i, N+1){
    REP(j,2*9*9){
      if(dp[i][j] == INF)
        cout<<'N';
      else
        cout<<dp[i][j];
      if((j+1)%9==0)cout<<' ';
    }
    cout<<endl;
  }*/
  REP(i, 2 * 9 * 9) { a = min(a, dp[1][i]); }
  cout << a << endl;
}

int main() {
  // cin.tie(0);

  while (input()) {
    solve();
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))

using namespace std;

#define pb push_back
#define mp make_pair
#define ft first
#define sd second

#define INF 1000000000
#define index(a, b, c) (a) * 81 + (b)*9 + (c)

int N;
char s[100004];
int dp[100004][2 * 9 * 9];

bool input() {
  cin >> s;
  if (s[0] == '#')
    return false;
  N = strlen(s);
  REP(i, N) s[i] -= '1';
  return true;
}

int x[9] = {0, 1, 2, 0, 1, 2, 0, 1, 2};

void solve() {
  REP(i, 2) {
    REP(j, 9) {
      REP(k, 9) {
        if (x[j] > x[k])
          dp[0][index(i, j, k)] = INF;
        else
          dp[0][index(i, j, k)] = 0;
      }
    }
  }

  REP(l, N) {
    REP(i, 2 * 9 * 9) dp[l + 1][i] = INF;

    REP(i, 2) {
      REP(j, 9) {
        REP(k, 9) {
          if (dp[l][index(i, j, k)] == INF)
            continue;
          if (x[s[l]] <= x[k])
            dp[l + 1][index(0, s[l], k)] = min(
                dp[l + 1][index(0, s[l], k)], dp[l][index(i, j, k)] + (i == 0));
          if (x[s[l]] >= x[j])
            dp[l + 1][index(1, j, s[l])] = min(
                dp[l + 1][index(1, j, s[l])], dp[l][index(i, j, k)] + (i == 1));
        }
      }
    }
  }

  int a = INF;
  /*REP(i, N+1){
    REP(j,2*9*9){
      if(dp[i][j] == INF)
        cout<<'N';
      else
        cout<<dp[i][j];
      if((j+1)%9==0)cout<<' ';
    }
    cout<<endl;
  }*/
  REP(i, 2 * 9 * 9) { a = min(a, dp[N][i]); }
  cout << a << endl;
}

int main() {
  // cin.tie(0);

  while (input()) {
    solve();
  }

  return 0;
}