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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for (int(i) = (a)-1; (i) >= 0; (i)--)
#define FORR(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template <typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const double EPS = 1e-9;

/*
 7 8 9
 4   6
 1 2 3
*/
set<int> se[3];
void init() {
  se[0].insert({1, 4, 7});
  se[1].insert({2, 8});
  se[2].insert({3, 6, 9});
}
string s;

int dfs(int nowlen, bool nextfoot, int nowleft, int nowright) { // t:right
                                                                // f:left
  if (s.length() == nowlen)
    return 0;
  const int nextpoint = s[nowlen] - '0';
  if (nextfoot) { // right
    for (int i = 0; i < 3; i++) {
      if (se[i].count(nowleft)) {
        if (i == 0) {
          return dfs(nowlen + 1, !nextfoot, nowleft, nextpoint);
        } else if (i == 1) {
          if (se[0].count(nextpoint)) {
            return 1 + dfs(nowlen + 1, nextfoot, nextpoint, nowright);
          } else {
            return dfs(nowlen + 1, !nextfoot, nowleft, nextpoint);
          }
        } else {
          if (se[2].count(nextpoint)) {
            return dfs(nowlen + 1, !nextfoot, nowleft, nextpoint);
          } else {
            return 1 + dfs(nowlen + 1, nextfoot, nextpoint, nowright);
          }
        }
      }
    }
    DEBUG(nowleft)
    assert(false);
  } else { // left
    for (int i = 0; i < 3; i++) {
      if (se[i].count(nowright)) {
        if (i == 0) {
          if (se[0].count(nextpoint)) {
            return dfs(nowlen + 1, !nextfoot, nextpoint, nowright);
          } else {
            return 1 + dfs(nowlen + 1, nextfoot, nowleft, nextpoint);
          }
        } else if (i == 1) {
          if (se[2].count(nextpoint)) {
            return 1 + dfs(nowlen + 1, nextfoot, nowleft, nextpoint);
          } else {
            return dfs(nowlen + 1, !nextfoot, nextpoint, nowright);
          }
        } else {
          return dfs(nowlen + 1, !nextfoot, nextpoint, nowright);
        }
      }
    }
    DEBUG(nowright)
    assert(false);
  }
}

void solve(string s) {
  int res = INF;
  for (int i = 1; i <= 9; i++) {   // left
    for (int j = 1; j <= 9; j++) { // right
      if (i != j) {
        if (i == 5 || j == 5)
          continue;
        if (se[1].count(j) && se[2].count(i))
          continue;
        if (se[0].count(j) && se[0].count(i) == 0)
          continue;
        if (se[1].count(i) && se[0].count(j))
          continue;
        if (se[2].count(i) && se[2].count(j) == 0)
          continue;
        chmin(res, min(dfs(0, true, i, j), dfs(0, false, i, j)));
      }
    }
  }
  printf("%d\\n", res);
}

signed main(void) {
  LL n, m, p, a, b, c, x, y, z, q;
  // string s;
  bool f = false;
  init();
  while (cin >> s, s != "#") {
    solve(s);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for (int(i) = (a)-1; (i) >= 0; (i)--)
#define FORR(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template <typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const double EPS = 1e-9;

/*
 7 8 9
 4   6
 1 2 3
*/
set<int> se[3];
void init() {
  se[0].insert({1, 4, 7});
  se[1].insert({2, 8});
  se[2].insert({3, 6, 9});
}

int dfs(string s, int nowlen, bool nextfoot, int nowleft,
        int nowright) { // t:right f:left
  if (s.length() == nowlen)
    return 0;
  const int nextpoint = s[nowlen] - '0';
  if (nextfoot) { // right
    for (int i = 0; i < 3; i++) {
      if (se[i].count(nowleft)) {
        if (i == 0) {
          return dfs(s, nowlen + 1, !nextfoot, nowleft, nextpoint);
        } else if (i == 1) {
          if (se[0].count(nextpoint)) {
            return 1 + dfs(s, nowlen + 1, nextfoot, nextpoint, nowright);
          } else {
            return dfs(s, nowlen + 1, !nextfoot, nowleft, nextpoint);
          }
        } else {
          if (se[2].count(nextpoint)) {
            return dfs(s, nowlen + 1, !nextfoot, nowleft, nextpoint);
          } else {
            return 1 + dfs(s, nowlen + 1, nextfoot, nextpoint, nowright);
          }
        }
      }
    }
    DEBUG(nowleft)
    assert(false);
  } else { // left
    for (int i = 0; i < 3; i++) {
      if (se[i].count(nowright)) {
        if (i == 0) {
          if (se[0].count(nextpoint)) {
            return dfs(s, nowlen + 1, !nextfoot, nextpoint, nowright);
          } else {
            return 1 + dfs(s, nowlen + 1, nextfoot, nowleft, nextpoint);
          }
        } else if (i == 1) {
          if (se[2].count(nextpoint)) {
            return 1 + dfs(s, nowlen + 1, nextfoot, nowleft, nextpoint);
          } else {
            return dfs(s, nowlen + 1, !nextfoot, nextpoint, nowright);
          }
        } else {
          return dfs(s, nowlen + 1, !nextfoot, nextpoint, nowright);
        }
      }
    }
    DEBUG(nowright)
    assert(false);
  }
}

void solve(string s) {
  int res = INF;
  for (int i = 1; i <= 9; i++) {   // left
    for (int j = 1; j <= 9; j++) { // right
      if (i != j) {
        if (i == 5 || j == 5)
          continue;
        if (se[1].count(j) && se[2].count(i))
          continue;
        if (se[0].count(j) && se[0].count(i) == 0)
          continue;
        if (se[1].count(i) && se[0].count(j))
          continue;
        if (se[2].count(i) && se[2].count(j) == 0)
          continue;
        chmin(res, min(dfs(s, 0, true, i, j), dfs(s, 0, false, i, j)));
      }
    }
  }
  printf("%d\\n", res);
}

signed main(void) {
  LL n, m, p, a, b, c, x, y, z, q;
  string s;
  bool f = false;
  init();
  while (cin >> s, s != "#") {
    solve(s);
  }
}