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

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

int x[6];
int n, c;
string S;

int hantei() {
  int A, B, C, D;
  if (S == "North") {
    A = x[0];
    B = x[1];
    C = x[5];
    D = x[4];
    x[0] = B;
    x[1] = C;
    x[5] = D;
    x[4] = A;
  }
  if (S == "South") {
    A = x[0];
    B = x[1];
    C = x[5];
    D = x[4];
    x[0] = D;
    x[1] = A;
    x[5] = B;
    x[4] = C;
  }
  if (S == "East") {
    A = x[0];
    B = x[2];
    C = x[5];
    D = x[3];
    x[0] = D;
    x[2] = A;
    x[5] = B;
    x[3] = C;
  }
  if (S == "West") {
    A = x[0];
    B = x[2];
    C = x[5];
    D = x[3];
    x[0] = B;
    x[2] = C;
    x[5] = D;
    x[3] = A;
  }
  if (S == "Left") {
    A = x[1];
    B = x[2];
    C = x[4];
    D = x[3];
    x[1] = D;
    x[2] = A;
    x[4] = B;
    x[3] = C;
  }
  if (S == "Right") {
    A = x[1];
    B = x[2];
    C = x[4];
    D = x[3];
    x[1] = B;
    x[2] = C;
    x[4] = D;
    x[3] = A;
  }
  return x[0];
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    x[0] = 1;
    x[1] = 2;
    x[2] = 3;
    x[3] = 4;
    x[4] = 5;
    x[5] = 6;
    c = 1;
    for (int i = 0; i < n; i++) {
      cin >> S;
      c += hantei();
    }
    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

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

int sai[100000][6];
int x[100000], n, sum;
int main() {
  while (true) {
    for (int i = 0; i < 6; i++) {
      sai[0][i] = i + 1;
    }
    string S[1000000];
    sum = 1;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> S[i];
    }
    for (int i = 0; i < n; i++) {
      if (S[i] == "North") {
        x[i + 1] = 0;
      }
      if (S[i] == "East") {
        x[i + 1] = 1;
      }
      if (S[i] == "South") {
        x[i + 1] = 2;
      }
      if (S[i] == "West") {
        x[i + 1] = 3;
      }
      if (S[i] == "Left") {
        x[i + 1] = 3;
      }
      if (S[i] == "Right") {
        x[i + 1] = 3;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (x[i] == 2) {
        sai[i][0] = sai[i - 1][4];
        sai[i][1] = sai[i - 1][0];
        sai[i][2] = sai[i - 1][2];
        sai[i][3] = sai[i - 1][3];
        sai[i][4] = sai[i - 1][5];
        sai[i][5] = sai[i - 1][1];
      }
      if (x[i] == 3) {
        sai[i][0] = sai[i - 1][2];
        sai[i][1] = sai[i - 1][1];
        sai[i][2] = sai[i - 1][5];
        sai[i][3] = sai[i - 1][0];
        sai[i][4] = sai[i - 1][4];
        sai[i][5] = sai[i - 1][3];
      }
      if (x[i] == 0) {
        sai[i][0] = sai[i - 1][1];
        sai[i][1] = sai[i - 1][5];
        sai[i][2] = sai[i - 1][2];
        sai[i][3] = sai[i - 1][3];
        sai[i][4] = sai[i - 1][0];
        sai[i][5] = sai[i - 1][4];
      }
      if (x[i] == 1) {
        sai[i][0] = sai[i - 1][3];
        sai[i][1] = sai[i - 1][1];
        sai[i][2] = sai[i - 1][0];
        sai[i][3] = sai[i - 1][5];
        sai[i][4] = sai[i - 1][4];
        sai[i][5] = sai[i - 1][2];
      }
      if (x[i] == 4) {
        sai[i][0] = sai[i - 1][0];
        sai[i][1] = sai[i - 1][3];
        sai[i][2] = sai[i - 1][1];
        sai[i][3] = sai[i - 1][4];
        sai[i][4] = sai[i - 1][2];
        sai[i][5] = sai[i - 1][5];
      }
      if (x[i] == 5) {
        sai[i][0] = sai[i - 1][0];
        sai[i][1] = sai[i - 1][2];
        sai[i][2] = sai[i - 1][4];
        sai[i][3] = sai[i - 1][1];
        sai[i][4] = sai[i - 1][3];
        sai[i][5] = sai[i - 1][5];
      }
      sum += sai[i][0];
    }
    cout << sum << endl;
  }
  return 0;
}