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

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cctype>
#include <fstream>
#include <locale>
#include <math.h>
#include <numeric>
#include <regex>
#include <stdlib.h>

// #include <map>

using namespace std;

enum s { t, f, r }; // top , front, right

class cDise {
  vector<int> X;
  vector<int> Y;
  vector<int> vLabel;
  vector<int> vRep;

  void rotateDice(char d) {
    int t;

    switch (d) {
    case 'N':
      t = X[0];
      X[0] = Y[0] = Y[1];
      Y[1] = 7 - t;
      break;
    case 'S':
      t = Y[1];
      Y[1] = Y[0];
      X[0] = Y[0] = 7 - t;
      break;
    case 'W':
      t = X[0];
      X[0] = Y[0] = X[1];
      X[1] = 7 - t;
      break;
    case 'E':
      t = X[1];
      X[1] = X[0];
      X[0] = Y[0] = 7 - t;
      break;
    default:
      break;
    }
  }

public:
  cDise(string l) {
    X = {1, 3};
    Y = {1, 2};

    istringstream iss(l);
    while (!iss.eof()) {
      int t;
      iss >> t;
      vLabel.push_back(t);
      iss.ignore();
    }

    for (int i = 0; i < vLabel.size(); i++) {
      vRep.push_back(0);
      for (int j = 0; j < vLabel.size(); j++) {
        if (vLabel.at(i) == vLabel.at(j) && i != j) {
          vRep.at(i)++;
        }
      }
    }
  }

  void rotateByStr(string dir) {
    istringstream iss(dir);
    vector<char> vc;
    int char_size = 1;

    for (string::iterator it = dir.begin(); it != dir.end(); it += char_size) {
      int t;
      rotateDice(*it);
    }
  }

  void rotateAndFindTop(string dir, int t) {
    istringstream iss(dir);
    vector<char> vc;
    int char_size = 1;

    for (string::iterator it = dir.begin(); it != dir.end(); it += char_size) {
      rotateDice(*it);
      if (getCurrentTop() == t)
        break;
    }
  }

  vector<int> getRep() { return vRep; }

  vector<int> getLabel() { return vLabel; }

  int getCurrentTop() {
    if (X[0] == Y[0]) {
      return vLabel.at(X[0] - 1);
    }
    return -1;
  }

  int getCurrentFront() {
    if (X[0] == Y[0]) {
      return vLabel.at(Y[1] - 1);
    }
    return -1;
  }

  int getCurrentRight() {
    if (X[0] == Y[0]) {
      return vLabel.at(X[1] - 1);
    }
    return -1;
  }
};

int main() {
  string lb;
  getline(cin, lb);
  cDise cd1(lb);
  getline(cin, lb);
  cDise cd2(lb);

  string qstr;
  int top, front;

  bool isSame;

  vector<int> v = cd1.getRep();

  for (int i = 0; i < v.size(); i++) {
    if (v.at(i) == 0) {
      cd1.rotateAndFindTop("NNNNENNN", cd1.getLabel().at(i));
      cd2.rotateAndFindTop("NNNNENNN", cd1.getCurrentTop());
      int first = cd2.getCurrentFront();
      while (cd2.getCurrentFront() != cd1.getCurrentFront()) {
        cd2.rotateByStr("NES");
        if (first == cd2.getCurrentFront()) {
          isSame = false;
          break;
        }
      }
      for (int j = 0; j < 4; j++) {
        cd1.rotateByStr("N");
        cd2.rotateByStr("N");
        if (cd1.getCurrentTop() == cd2.getCurrentTop() &&
            cd1.getCurrentFront() == cd2.getCurrentFront() &&
            cd1.getCurrentRight() == cd2.getCurrentRight()) {
          isSame = true;
        } else {
          isSame = false;
          break;
        }
      }
    }
  }

  string ans(isSame ? "Yes" : "No");
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cctype>
#include <fstream>
#include <locale>
#include <math.h>
#include <numeric>
#include <regex>
#include <stdlib.h>

// #include <map>

using namespace std;

enum s { t, f, r }; // top , front, right

class cDise {

  vector<int> X;
  vector<int> Y;
  vector<int> vLabel;
  int max_index;
  int min_index;

  void rotateDice(char d) {
    int t;

    switch (d) {
    case 'N':
      t = X[0];
      X[0] = Y[0] = Y[1];
      Y[1] = 7 - t;
      break;
    case 'S':
      t = Y[1];
      Y[1] = Y[0];
      X[0] = Y[0] = 7 - t;
      break;
    case 'W':
      t = X[0];
      X[0] = Y[0] = X[1];
      X[1] = 7 - t;
      break;
    case 'E':
      t = X[1];
      X[1] = X[0];
      X[0] = Y[0] = 7 - t;
      break;
    default:
      break;
    }
  }

public:
  cDise(string l) {
    X = {1, 3};
    Y = {1, 2};

    int max = 0;

    istringstream iss(l);
    while (!iss.eof()) {
      int t;
      iss >> t;
      vLabel.push_back(t);
      iss.ignore();
    }
    max_index = *max_element(vLabel.begin(), vLabel.end());
    min_index = *min_element(vLabel.begin(), vLabel.end());
  }

  void rotateByStr(string dir) {
    istringstream iss(dir);
    vector<char> vc;
    int char_size = 1;

    for (string::iterator it = dir.begin(); it != dir.end(); it += char_size) {
      int t;
      rotateDice(*it);
    }
  }

  void rotateAndFindTop(string dir, int t) {
    istringstream iss(dir);
    vector<char> vc;
    int char_size = 1;

    for (string::iterator it = dir.begin(); it != dir.end(); it += char_size) {
      rotateDice(*it);
      if (getCurrentTop() == t)
        break;
    }
  }

  void rotateToMax() {
    while (getCurrentTop() != max_index) {
      rotateAndFindTop("NNNNENNN", max_index);
    }
  }

  int getCurrentTop() {
    if (X[0] == Y[0]) {
      return vLabel.at(X[0] - 1);
    }
    return -1;
  }

  int getCurrentFront() {
    if (X[0] == Y[0]) {
      return vLabel.at(Y[1] - 1);
    }
    return -1;
  }

  int getCurrentRight() {
    if (X[0] == Y[0]) {
      return vLabel.at(X[1] - 1);
    }
    return -1;
  }
};

int main() {
  string lb;
  getline(cin, lb);
  cDise cd1(lb);
  getline(cin, lb);
  cDise cd2(lb);

  cd1.rotateToMax();
  cd2.rotateToMax();

  while (cd2.getCurrentFront() != cd1.getCurrentFront()) {
    cd2.rotateByStr("NES");
  }

  bool isSame = true;

  for (int i = 0; i < 4; i++) {
    cd1.rotateByStr("N");
    cd2.rotateByStr("N");
    if (cd1.getCurrentTop() != cd2.getCurrentTop() ||
        cd1.getCurrentFront() != cd2.getCurrentFront() ||
        cd1.getCurrentRight() != cd2.getCurrentRight()) {
      isSame = false;
      break;
    }
  }
  string ans(isSame ? "Yes" : "No");
  cout << ans << endl;

  return 0;
}