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 <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define MAXS (9 * 9 * 2)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
int rl[] = {0, 1, 2, 0, 1, 2, 0, 1, 2};
int dp[100001][MAXS];
char s[200000];
#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 (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define INF 1000000000

int soeji(int a, int b, int c) { return a * 9 * 2 + b * 2 + c; }

int main() {
  while (1) {
    scanf("%s", s);
    if (s[0] == '#')
      break;

    int n = strlen(s);
    REP(i, n)
    s[i] -= '1';

    REP(i, MAXS)
    dp[n][i] = 0;

    RREP(i, n) {
      REP(a, 9) {
        REP(b, 9) {
          REP(c, 2) {
            int a1 = a, b1 = b, a2 = a, b2 = b;
            if (c == 0) {
              a1 = s[i];
              b2 = s[i];
            } else {
              b1 = s[i];
              a2 = s[i];
            }
            int x = rl[a1] >= rl[b1] ? dp[i + 1][soeji(a1, b2, c)] : INF - 1;
            int y = rl[a2] >= rl[b2] ? dp[i + 1][soeji(a2, b2, !c)] : INF;
            dp[i][soeji(a, b, c)] = min(x + 1, y);
          }
        }
      }
    }
    int mini = 1000000000;
    REP(i, 9 * 9 * 2) {
      if (dp[0][i] < mini)
        mini = dp[0][i];
    }
    printf("%d\\n", mini);
  }
}