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

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

using namespace std;

//   swap(d[], d[]);
void rot1(vector<int> &d) {
  swap(d[1], d[2]);
  swap(d[2], d[4]);
  swap(d[4], d[3]);
}
void rot2(vector<int> &d) {
  swap(d[1], d[0]);
  swap(d[0], d[4]);
  swap(d[4], d[5]);
}
void rot3(vector<int> &d) {
  swap(d[0], d[3]);
  swap(d[3], d[5]);
  swap(d[5], d[2]);
}
void rev1(vector<int> &d) {
  swap(d[1], d[3]);
  swap(d[3], d[4]);
  swap(d[4], d[2]);
}
void rev2(vector<int> &d) {
  swap(d[1], d[5]);
  swap(d[5], d[4]);
  swap(d[4], d[0]);
}
void rev3(vector<int> &d) {
  swap(d[0], d[2]);
  swap(d[2], d[5]);
  swap(d[5], d[3]);
}

int main() {
  int ttt = 0;
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int cnt = 0;
    map<string, int> memo;
    vector<int> d[n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 6; j++) {
        string s;
        cin >> s;
        if (memo.find(s) == memo.end()) {
          d[i].push_back(cnt);
          memo[s] = cnt;
          cnt++;
        } else {
          d[i].push_back(memo[s]);
        }
      }
    }
    if (n == 1) {
      cout << 0 << endl;
      continue;
    }

    set<vector<vector<int>>> cut;
    stack<vector<vector<int>>> st;
    int ans = INT_MAX;
    bool same = true;
    for (int i = 0; i < 6; i++) {
      for (int j = 1; j < n; j++) {
        if (d[j][i] != d[j - 1][i]) {
          same = false;
          break;
        }
      }
      if (!same)
        break;
    }
    if (same) {
      ans = 0;
    }
    vector<vector<int>> t;
    for (int i = 0; i < n; i++) {
      t.push_back(d[i]);
    }
    st.push(t);
    cut.insert(t);
    while (!st.empty()) {
      vector<vector<int>> np = st.top();
      st.pop();

      for (int i = 1; i < n; i++) {
        for (int ch = 0; ch < 6; ch++) {
          vector<vector<int>> tnp = np;
          if (ch == 0) {
            rot1(tnp[i]);
          } else if (ch == 1) {
            rot2(tnp[i]);
          } else if (ch == 2) {
            rot3(tnp[i]);
          } else if (ch == 3) {
            rev1(tnp[i]);
          } else if (ch == 4) {
            rev2(tnp[i]);
          } else if (ch == 5) {
            rev3(tnp[i]);
          }
          if (cut.find(tnp) != cut.end()) {
            continue;
          }
          st.push(tnp);
          cut.insert(tnp);
          int tsum = 0;
          for (int j = 0; j < 6; j++) {
            int tcnt[cnt];
            memset(tcnt, 0, sizeof(tcnt));
            int maxp = 0;
            for (int k = 0; k < n; k++) {
              tcnt[tnp[k][j]]++;
              if (tcnt[tnp[k][j]] > tcnt[maxp]) {
                maxp = tnp[k][j];
              }
            }
            tsum += n - tcnt[maxp];
          }
          ans = min(ans, tsum);
        }
      }
    }
    int tsum = 0;
    for (int j = 0; j < 6; j++) {
      int tcnt[cnt];
      memset(tcnt, 0, sizeof(tcnt));
      int maxp = 0;
      for (int k = 0; k < n; k++) {
        tcnt[d[k][j]]++;
        if (tcnt[d[k][j]] > tcnt[maxp]) {
          maxp = d[k][j];
        }
      }
      tsum += n - tcnt[maxp];
    }

    cout << min(ans, tsum) << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

using namespace std;

//   swap(d[], d[]);
void rot1(vector<int> &d) {
  swap(d[1], d[2]);
  swap(d[2], d[4]);
  swap(d[4], d[3]);
}
void rot2(vector<int> &d) {
  swap(d[1], d[0]);
  swap(d[5], d[1]);
  swap(d[4], d[5]);
}
void rot3(vector<int> &d) {
  swap(d[0], d[3]);
  swap(d[3], d[5]);
  swap(d[5], d[2]);
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int cnt = 0;
    map<string, int> memo;
    vector<int> d[n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 6; j++) {
        string s;
        cin >> s;
        if (memo.find(s) == memo.end()) {
          d[i].push_back(cnt);
          memo[s] = cnt;
          cnt++;
        } else {
          d[i].push_back(memo[s]);
        }
      }
    }
    if (n == 1) {
      cout << 0 << endl;
      continue;
    }

    int move[] = {1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1,
                  1, 1, 1, 2, 3, 1, 1, 1, 1, 3, 3, 1, 1, 1, 1};
    set<vector<int>> cut;
    stack<vector<int>> st;
    vector<int> t;
    int ans = INT_MAX;
    stack<vector<int>> dst;
    for (int i = 0; i < n; i++) {
      t.push_back(0);
    }
    st.push(t);
    for (int i = 0; i < n; i++) {
      vector<int> dt;
      for (int j = 0; j < 6; j++) {
        dt.push_back(d[i][j]);
      }
      dst.push(dt);
    }
    int count = 0;
    while (!st.empty()) {
      count++;
      vector<int> p = st.top();
      st.pop();
      vector<int> nd[n];
      for (int i = 0; i < n; i++) {
        nd[i] = dst.top();
        dst.pop();
      }
      int tcnt = 0;
      for (int i = 0; i < n; i++) {
        if (p[i] < 31) {
          p[i]++;
          if (cut.find(p) != cut.end())
            continue;
          st.push(p);
          cut.insert(p);
          p[i]--;

          vector<int> ndt[n];
          for (int j = 0; j < n; j++) {
            ndt[j] = nd[j];
          }

          switch (move[p[i]]) {
          case 1:
            rot1(ndt[i]);
            break;
          case 2:
            rot2(ndt[i]);
            break;
          case 3:
            rot3(ndt[i]);
            break;
          }

          for (int j = 0; j < 6; j++) {
            int cntmemo[cnt];
            memset(cntmemo, 0, sizeof(cntmemo));
            int maxp = 0;
            for (int k = 0; k < n; k++) {
              cntmemo[ndt[k][j]]++;
              if (cntmemo[ndt[k][j]] > cntmemo[maxp])
                maxp = ndt[k][j];
            }
            tcnt += n - cntmemo[maxp];
            if (tcnt >= ans)
              break;
          }
          ans = min(ans, tcnt);
          for (int j = 0; j < n; j++)
            dst.push(ndt[j]);
        }
      }
    }
    // cout << count << endl;
    cout << ans << endl;
  }
}