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

#include <iostream>
using namespace std;
// J=1 O=2 I=4
int main() {
  int n;
  cin >> n;
  char skn[1001];
  for (int i = 1; i <= n; i++) {
    cin >> skn[i];
  }

  int keka[1001][8];

  keka[0][1] = 1;
  keka[0][2] = 0;
  keka[0][3] = 0;
  keka[0][4] = 0;
  keka[0][5] = 0;
  keka[0][6] = 0;
  keka[0][7] = 0;

  for (int day = 1; day <= n; day++) {
    // J
    if (skn[day] == 'J') {
      keka[day][1] = keka[day - 1][1] + keka[day - 1][3] + keka[day - 1][5] +
                     keka[day - 1][7];
    } else {
      keka[day][1] = 0;
    }
    // O
    if (skn[day] == 'O') {
      keka[day][2] = keka[day - 1][2] + keka[day - 1][3] + keka[day - 1][6] +
                     keka[day - 1][7];
    } else {
      keka[day][2] = 0;
    }
    // I
    if (skn[day] == 'I') {
      keka[day][4] = keka[day - 1][4] + keka[day - 1][5] + keka[day - 1][6] +
                     keka[day - 1][7];
    } else {
      keka[day][4] = 0;
    }
    // J+O
    if (skn[day] == 'J' || skn[day] == 'O') {
      keka[day][3] = keka[day - 1][1] + keka[day - 1][2] + keka[day - 1][3] +
                     keka[day - 1][5] + keka[day - 1][6] + keka[day - 1][7];
    } else {
      keka[day][3] = 0;
    }
    // J+I
    if (skn[day] == 'J' || skn[day] == 'I') {
      keka[day][5] = keka[day - 1][1] + keka[day - 1][3] + keka[day - 1][4] +
                     keka[day - 1][5] + keka[day - 1][6] + keka[day - 1][7];
    } else {
      keka[day][5] = 0;
    }
    // O+I
    if (skn[day] == 'O' || skn[day] == 'I') {
      keka[day][6] = keka[day - 1][2] + keka[day - 1][3] + keka[day - 1][4] +
                     keka[day - 1][5] + keka[day - 1][6] + keka[day - 1][7];
    } else {
      keka[day][6] = 0;
    }
    // J+O+I
    keka[day][7] = keka[day - 1][1] + keka[day - 1][2] + keka[day - 1][3] +
                   keka[day - 1][4] + keka[day - 1][5] + keka[day - 1][6] +
                   keka[day - 1][7];

    for (int i = 1; i < 8; i++) {
      keka[day][i] %= 10007;
    }
  }
  int sum = 0;
  for (int i = 1; i < 8; i++) {
    sum += keka[n][i];
  }
  cout << sum % 10007 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#define MAX 1000
using namespace std;

enum { J = 1, O, J_O, I, J_I, O_I, J_O_I };

int main() {
  int n, cnt;
  char ire[MAX];
  int koho[4];
  int ary[MAX][8];

  cin >> n;
  for (int i = 0; i < n; i++) {
    ire[i] = getchar();
  }

  for (int i = 0; i < n; i) {
    for (int j = 0; j < 8; j++) {
      ary[i][j] = 0;
    }
  }
  ary[0][J] = 1;

  for (int day = 0; day < (n - 1); day++) {

    switch (ire[day]) {
    case 'J':
      koho[0] = J;
      koho[1] = J_O;
      koho[2] = J_I;
      koho[3] = J_O_I;
      break;
    case 'O':
      koho[0] = O;
      koho[1] = J_O;
      koho[2] = O_I;
      koho[3] = J_O_I;
      break;
    case 'I':
      koho[0] = I;
      koho[1] = J_I;
      koho[2] = O_I;
      koho[3] = J_O_I;
      break;
    }

    for (int ba = 0; ba < 8; ba++) {
      for (int kn = 0; kn < 4; kn++) {
        cnt = 0;
        switch (koho[kn]) {
        case 0:
          break;
        case J:
          cnt += ary[day][J] + ary[day][J_O] + ary[day][J_I] + ary[day][J_O_I];
          break;
        case O:
          cnt += ary[day][O] + ary[day][J_O] + ary[day][O_I] + ary[day][J_O_I];
          break;
        case I:
          cnt += ary[day][I] + ary[day][J_I] + ary[day][O_I] + ary[day][J_O_I];
          break;
        case J_O:
          cnt += ary[day][J] + ary[day][O] + ary[day][J_O] + ary[day][O_I] +
                 ary[day][J_I] + ary[day][J_O_I];
          break;
        case J_I:
          cnt += ary[day][J] + ary[day][I] + ary[day][J_O] + ary[day][O_I] +
                 ary[day][J_I] + ary[day][J_O_I];
          break;
        case O_I:
          cnt += ary[day][I] + ary[day][O] + ary[day][J_O] + ary[day][O_I] +
                 ary[day][J_I] + ary[day][J_O_I];
          break;
        }
      }
      if (day >> 4)
        ary[day + 1][ba] = (cnt % 10007);
    }
  }
  cnt = 0;
  for (int i = 0; i < 8; i++) {
    cnt = ary[n - 1][i];
  }
  cout << (cnt % 10007) << endl;
}