Problem p00518 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main(void) {
  int N;
  string s;
  int respon[1000];
  int dp[1000][8];
  cin >> N;
  cin >> s;
  REP(i, s.size()) {
    if (s[i] == 'J')
      respon[i] = 1;
    else if (s[i] == 'O')
      respon[i] = 2;
    else
      respon[i] = 4;
  }
  REP(i, 1000) { fill(dp[i], dp[i] + 8, 0); }

  REP(i, 8) {
    if ((i & respon[0]) != 0 && (i & 1) != 0)
      dp[0][i] = 1;
  }

  FOR(i, 1, N) {
    REP(j, 8) {
      if ((j & respon[i]) == 0)
        continue;
      REP(k, 8) {
        if ((j & k) == 0)
          continue;
        dp[i][j] = (dp[i][j] + dp[i - 1][k]) % 10007;
      }
    }
  }

  int res = 0;
  REP(i, 8) { res = (res + dp[N - 1][i]) % 10007; }
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main(void) {
  int N;
  string s;
  int respon[100];
  int dp[100][8];
  cin >> N;
  cin >> s;
  REP(i, s.size()) {
    if (s[i] == 'J')
      respon[i] = 1;
    else if (s[i] == 'O')
      respon[i] = 2;
    else
      respon[i] = 4;
  }
  REP(i, 100) { fill(dp[i], dp[i] + 8, 0); }

  REP(i, 8) {
    if ((i & respon[0]) != 0 && (i & 1) != 0)
      dp[0][i] = 1;
  }

  FOR(i, 1, N) {
    REP(j, 8) {
      if ((j & respon[i]) == 0)
        continue;
      REP(k, 8) {
        if ((j & k) == 0)
          continue;
        dp[i][j] = (dp[i][j] + dp[i - 1][k]) % 10007;
      }
    }
  }

  int res = 0;
  REP(i, 8) { res = (res + dp[N - 1][i]) % 10007; }
  cout << res << endl;
}