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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  const int idx1[] = {0, 4, 5, 1, 3, 7, 6, 2};
  const int idx2[] = {0, 3, 7, 4, 1, 2, 6, 5};
  auto equal = [&](const vector<int> &x, const vector<int> &y) {
    auto equal_ = [](const vector<int> &a, vector<int> b) {
      auto equal__ = [&] {
        rep(i, 0, 4) {
          bool ok = true;
          rep(j, 0,
              4) if (a[j] != b[(i + j) % 4] or
                     a[4 + j] != b[(4 + i + j >= 8 ? i + j : 4 + i + j)]) {
            ok = false;
            break;
          }
          if (ok)
            return true;
        }
        return false;
      };
      if (equal__())
        return true;
      reverse(b.begin(), b.end());
      return equal__();
    };
    if (equal_(x, y))
      return true;
    vector<int> tmp(8);
    rep(i, 0, 8) tmp[i] = y[idx1[i]];
    if (equal_(x, tmp))
      return true;
    rep(i, 0, 8) tmp[i] = y[idx2[i]];
    return equal_(x, tmp);
  };
  string tmp;
  while (cin >> tmp) {
    vector<string> color(8);
    color[0] = tmp;
    rep(i, 1, 8) cin >> color[i];

    map<string, int> mp;
    int cnt = 0;
    rep(i, 0, 8) if (mp.find(color[i]) == mp.end()) {
      mp[color[i]] = cnt;
      ++cnt;
    }
    vector<int> v(8);
    rep(i, 0, 8) v[i] = mp[color[i]];
    sort(v.begin(), v.end());

    vector<vector<int>> all;
    do {
      bool ok = true;
      for (const auto &a : all)
        if (equal(v, a)) {
          ok = false;
          break;
        }
      if (ok)
        all.emplace_back(v);
    } while (next_permutation(v.begin(), v.end()));

    cout << all.size() << endl;
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(8);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  const int idx1[] = {0, 4, 5, 1, 3, 7, 6, 2};
  const int idx2[] = {0, 3, 7, 4, 1, 2, 6, 5};
  auto equal = [&](const vector<int> &x, const vector<int> &y) {
    auto equal_ = [](vector<int> a, vector<int> b) {
      auto equal__ = [&] {
        rep(i, 0, 4) {
          bool ok = true;
          rep(j, 0,
              4) if (a[j] != b[(i + j) % 4] or
                     a[4 + j] != b[(4 + i + j >= 8 ? i + j : 4 + i + j)]) {
            ok = false;
            break;
          }
          if (ok)
            return true;
        }
        return false;
      };
      if (equal__())
        return true;
      reverse(b.begin(), b.end());
      return equal__();
    };
    if (equal_(x, y))
      return true;
    vector<int> tmp(8);
    rep(i, 0, 8) tmp[i] = y[idx1[i]];
    if (equal_(x, tmp))
      return true;
    rep(i, 0, 8) tmp[i] = y[idx2[i]];
    return equal_(x, tmp);
  };
  string tmp;
  while (cin >> tmp) {
    vector<string> color(8);
    color[0] = tmp;
    rep(i, 1, 8) cin >> color[i];

    map<string, int> mp;
    int cnt = 0;
    rep(i, 0, 8) if (mp.find(color[i]) == mp.end()) {
      mp[color[i]] = cnt;
      ++cnt;
    }
    vector<int> v(8);
    rep(i, 0, 8) v[i] = mp[color[i]];
    sort(v.begin(), v.end());

    vector<vector<int>> all;
    do {
      bool ok = true;
      for (const auto &a : all)
        if (equal(v, a)) {
          ok = false;
          break;
        }
      if (ok)
        all.emplace_back(v);
    } while (next_permutation(v.begin(), v.end()));

    cout << all.size() << endl;
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(8);
  solve();
  return 0;
}