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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

string dice[8];
bool use[8];
string cube[8];

char trans(char a) {
  if ('a' <= a && a <= 'z')
    return a - 32;
  else
    return a + 32;
}

bool rec(int times) {
  if (times == 8)
    return true;
  for (int i = 0; i < 8; i++) {
    if (!use[i]) {
      bool f = true;
      char ex;
      if (times % 2) {
        bool f2 = false;
        ex = trans(cube[times - 1][5]);
        for (int j = 0; j < 6; j++)
          if (dice[i][j] == ex)
            f2 = true;
        f &= f2;
      }

      if (times % 4 >= 2) {
        bool f2 = false;
        ex = trans(cube[times - 2][3]);
        for (int j = 0; j < 6; j++)
          if (dice[i][j] == ex)
            f2 = true;
        f &= f2;
      }
      if (times >= 4) {
        bool f2 = false;
        ex = trans(cube[times - 4][4]);
        for (int j = 0; j < 6; j++)
          if (dice[i][j] == ex)
            f2 = true;
        f &= f2;
      }
      if (f) {
        use[i] = true;
        string tmp = dice[i];
        for (int l = 0; l < 3; l++) {
          if (l == 1) {
            swap(tmp[0], tmp[5]);
            swap(tmp[1], tmp[4]);
          }
          if (l == 2) {
            swap(tmp[0], tmp[5]);
            swap(tmp[2], tmp[3]);
          }
          for (int j = 0; j < 3; j++) {
            swap(tmp[0], tmp[1]);
            swap(tmp[1], tmp[2]);
            swap(tmp[5], tmp[4]);
            swap(tmp[4], tmp[3]);
            for (int k = 0; k < 4; k++) {
              swap(tmp[1], tmp[2]);
              swap(tmp[2], tmp[4]);
              swap(tmp[4], tmp[3]);

              // if(!i && !times)cout << tmp << endl;
              f = true;
              if (times % 2) {
                if (trans(tmp[0]) != cube[times - 1][5])
                  f = false;
              }
              if (times % 4 >= 2) {
                if (trans(tmp[2]) != cube[times - 2][3])
                  f = false;
              }
              if (times >= 4) {
                if (trans(tmp[1]) != cube[times - 4][4])
                  f = false;
              }
              if (f) {
                cube[times] = tmp;
                if (rec(times + 1))
                  return true;
              }
            }
          }
        }
        use[i] = false;
      }
    }
  }

  return false;
}

int main() {
  for (;;) {
    cin >> dice[0];
    if (dice[0] == "0")
      break;
    for (int i = 1; i < 8; i++)
      cin >> dice[i];
    for (int i = 0; i < 8; i++)
      use[i] = false;

    if (rec(0))
      cout << "YES\\n";
    else
      cout << "NO\\n";
    // for(int i=0;i<8;i++)cout << cube[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

string dice[8];
bool use[8];
string cube[8];

char trans(char a) {
  if ('a' <= a && a <= 'z')
    return a - 32;
  else
    return a + 32;
}

bool rec(int times) {
  if (times == 8)
    return true;
  for (int i = 0; i < 8; i++) {
    if (!use[i]) {
      bool f = true;
      char ex;
      if (times % 2) {
        bool f2 = false;
        ex = trans(cube[times - 1][5]);
        for (int j = 0; j < 6; j++)
          if (dice[i][j] == ex)
            f2 = true;
        f &= f2;
      }

      if (times % 4 >= 2) {
        bool f2 = false;
        ex = trans(cube[times - 2][3]);
        for (int j = 0; j < 6; j++)
          if (dice[i][j] == ex)
            f2 = true;
        f &= f2;
      }
      if (times >= 4) {
        bool f2 = false;
        ex = trans(cube[times - 4][4]);
        for (int j = 0; j < 6; j++)
          if (dice[i][j] == ex)
            f2 = true;
        f &= f2;
      }
      if (f) {
        use[i] = true;
        string tmp = dice[i];
        for (int l = 0; l < 2; l++) {
          if (l) {
            swap(tmp[0], tmp[5]);
            swap(tmp[1], tmp[4]);
            swap(tmp[2], tmp[3]);
          }
          for (int j = 0; j < 3; j++) {
            if (j) {
              string hoge;
              hoge.push_back(tmp[1]);
              hoge.push_back(tmp[2]);
              hoge.push_back(tmp[0]);
              hoge.push_back(tmp[5]);
              hoge.push_back(tmp[3]);
              hoge.push_back(tmp[4]);
              tmp = hoge;
            }
            for (int k = 0; k < 4; k++) {
              if (k) {
                string hoge;
                hoge.push_back(tmp[0]);
                hoge.push_back(tmp[2]);
                hoge.push_back(tmp[4]);
                hoge.push_back(tmp[3]);
                hoge.push_back(tmp[1]);
                hoge.push_back(tmp[5]);
                tmp = hoge;
              }
              // if(!i && !times)cout << tmp << endl;
              f = true;
              if (times % 2) {
                if (trans(tmp[0]) != cube[times - 1][5])
                  f = false;
              }
              if (times % 4 >= 2) {
                if (trans(tmp[2]) != cube[times - 2][3])
                  f = false;
              }
              if (times >= 4) {
                if (trans(tmp[1]) != cube[times - 4][4])
                  f = false;
              }
              if (f) {
                cube[times] = tmp;
                if (rec(times + 1))
                  return true;
              }
            }
          }
        }
        use[i] = false;
      }
    }
  }

  return false;
}

int main() {
  for (;;) {
    cin >> dice[0];
    if (dice[0] == "0")
      break;
    for (int i = 1; i < 8; i++)
      cin >> dice[i];
    for (int i = 0; i < 8; i++)
      use[i] = false;

    if (rec(0))
      cout << "YES\\n";
    else
      cout << "NO\\n";
    // for(int i=0;i<8;i++)cout << cube[i] << endl;
  }
}