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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

struct P {
  vector<int> v;
  P(vector<int> &v) : v(v) {}
  P rotate1() {
    P r = *this;
    REP(i, 3) swap(r.v[i], r.v[(i + 1) % 4]);
    REP(i, 3) swap(r.v[4 + i], r.v[4 + (i + 1) % 4]);
    return r;
  }
  P rotate2() {
    vector<int> w = this->v;
    swap(w[0], w[4]);
    swap(w[4], w[5]);
    swap(w[5], w[1]);
    swap(w[2], w[3]);
    swap(w[3], w[7]);
    swap(w[7], w[6]);
    return P(w);
  }
  P rotate3() {
    vector<int> w = this->v;
    swap(w[0], w[4]);
    swap(w[4], w[7]);
    swap(w[7], w[3]);
    swap(w[1], w[5]);
    swap(w[5], w[6]);
    swap(w[6], w[2]);
    return P(w);
  }
  vector<P> all() {
    P r = *this;
    vector<P> res;
    for (int i = 0; i < 6; ++i, r = (i & 2 ? r.rotate2() : r.rotate3())) {
      for (int j = 0; j < 4; ++j, r = r.rotate1())
        res.push_back(r);
    }
    return res;
  }
};
int main() {
  string s;
  while (cin >> s) {
    map<string, int> mp;
    int num = 0;
    vector<int> v;
    mp[s] = num++;
    v.push_back(mp[s]);
    REP(i, 7) {
      cin >> s;
      if (mp.count(s) == 0)
        mp[s] = num++;
      v.push_back(mp[s]);
    }
    vector<int> cnt(24);
    sort(ALL(v));
    do {
      vector<P> ret = P(v).all();
      REP(i, 24) {
        if (ret[i].v == v)
          cnt[i]++;
      }
    } while (next_permutation(ALL(v)));
    int ans = 0;
    REP(i, 24) ans += cnt[i];
    cout << ans / 24 << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

struct P {
  vector<int> v;
  P(vector<int> &v) : v(v) {}
  P rotate1() {
    P r = *this;
    REP(i, 3) swap(r.v[i], r.v[(i + 1) % 4]);
    REP(i, 3) swap(r.v[4 + i], r.v[4 + (i + 1) % 4]);
    return r;
  }
  P rotate2() {
    vector<int> w = this->v;
    swap(w[0], w[4]);
    swap(w[4], w[5]);
    swap(w[5], w[1]);
    swap(w[2], w[3]);
    swap(w[3], w[7]);
    swap(w[7], w[6]);
    return P(w);
  }
  P rotate3() {
    vector<int> w = this->v;
    swap(w[0], w[4]);
    swap(w[4], w[7]);
    swap(w[7], w[3]);
    swap(w[1], w[5]);
    swap(w[5], w[6]);
    swap(w[6], w[2]);
    return P(w);
  }
  vector<P> all() {
    P r = *this;
    vector<P> res;
    for (int i = 0; i < 6; ++i, r = (i & 2 ? r.rotate2() : r.rotate3())) {
      for (int j = 0; j < 4; ++j, r = r.rotate1())
        res.push_back(r);
    }
    return res;
  }
};

const bool operator<(P a, const P &b) {
  vector<P> v = a.all();
  vector<int> mi = a.v;
  FOR(it, v) {
    if (it->v == b.v)
      return 0;
    mi = min(mi, it->v);
  }
  return mi < b.v;
}

int main() {
  string s;
  while (cin >> s) {
    map<string, int> mp;
    int num = 0;
    vector<int> v;
    mp[s] = num++;
    v.push_back(mp[s]);
    REP(i, 7) {
      cin >> s;
      if (mp.count(s) == 0)
        mp[s] = num++;
      v.push_back(mp[s]);
    }
    // set<P> hoge;
    // int arr[8] = {0,1,2,3,4,5,6,7};
    // vector<int> vv(arr,arr+8);
    // cout << ": " << (P(v) < P(vv)) << endl;
    // cout << ": " << (P(vv) < P(v)) << endl;
    // hoge.insert(P(v));
    // hoge.insert(P(vv));
    // cout << hoge.size() << endl;
    // vector<P> a = P(vv).all();
    // FOR(it, a) {
    //   FOR(jt, it->v) cout << *jt << " ";
    //   cout << endl;
    // }
    set<P> se;
    do {
      REP(i, 8) { se.insert(P(v)); }
    } while (next_permutation(ALL(v)));
    // FOR(it, se) {
    //   FOR(jt, it->v) cout << *jt; cout << endl;
    // }
    cout << se.size() << endl;
  }
}