Problem p01287 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
void rotate(vector<int> &v, int a, int b, int c, int d) {
  int t = v[a];
  v[a] = v[b];
  v[b] = v[c];
  v[c] = v[d];
  v[d] = t;
}
void rotate_x(vector<int> &v) {
  rotate(v, 1, 2, 6, 5);
  rotate(v, 0, 3, 7, 4);
}
void rotate_y(vector<int> &v) {
  rotate(v, 0, 1, 5, 4);
  rotate(v, 3, 2, 6, 7);
}
void rotate_z(vector<int> &v) {
  rotate(v, 0, 1, 2, 3);
  rotate(v, 4, 5, 6, 7);
}
bool equal(const vector<int> &d, vector<int> e) {
  for (int i = 0; i < 6; i++) {
    if (i & 1)
      rotate_x(e);
    else
      rotate_y(e);
    for (int j = 0; j < 4; j++) {
      rotate_z(e);
      if (d == e)
        return true;
    }
  }
  return false;
}
void all_insert(set<vector<int>> &s, vector<int> e) {
  for (int i = 0; i < 6; i++) {
    if (i & 1)
      rotate_x(e);
    else
      rotate_y(e);
    for (int j = 0; j < 4; j++) {
      rotate_z(e);
      s.insert(e);
    }
  }
}
int main() {
  string line;
  while (getline(cin, line)) {
    string s;
    stringstream ss(line);
    map<string, int> id;
    vector<int> d(8);
    REP(i, 8) {
      ss >> s;
      if (id.count(s)) {
        d[i] = id[s];
      } else {
        int t = id.size();
        d[i] = id[s] = t;
      }
    }
    sort(d.begin(), d.end());
    int ans = 0;
    set<vector<int>> check;
    do {
      bool ok = true;
      if (!check.count(d)) {
        ans++;
        all_insert(check, d);
      }
    } while (next_permutation(d.begin(), d.end()));
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
void rotate(vector<int> &v, int a, int b, int c, int d) {
  int t = v[a];
  v[a] = v[b];
  v[b] = v[c];
  v[c] = v[d];
  v[d] = t;
}
void rotate_x(vector<int> &v) {
  rotate(v, 1, 2, 6, 5);
  rotate(v, 0, 3, 7, 4);
}
void rotate_y(vector<int> &v) {
  rotate(v, 0, 1, 5, 4);
  rotate(v, 3, 2, 6, 7);
}
void rotate_z(vector<int> &v) {
  rotate(v, 0, 1, 2, 3);
  rotate(v, 4, 5, 6, 7);
}
bool equal(const vector<int> &d, vector<int> e) {
  for (int i = 0; i < 6; i++) {
    if (i & 1)
      rotate_x(e);
    else
      rotate_y(e);
    for (int j = 0; j < 4; j++) {
      rotate_z(e);
      if (d == e)
        return true;
    }
  }
  return false;
}
int main() {
  string line;
  while (getline(cin, line)) {
    string s;
    stringstream ss(line);
    map<string, int> id;
    vector<int> d(8);
    REP(i, 8) {
      ss >> s;
      if (id.count(s)) {
        d[i] = id[s];
      } else {
        int t = id.size();
        d[i] = id[s] = t;
      }
    }
    sort(d.begin(), d.end());
    vector<vector<int>> ans;
    do {
      bool ok = true;
      REP(i, ans.size()) if (equal(ans[i], d)) {
        ok = false;
        break;
      }
      if (ok)
        ans.push_back(d);
    } while (next_permutation(d.begin(), d.end()));
    cout << ans.size() << endl;
  }
  return 0;
}