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

#include <iostream>
// j=前の日,1=J,11=JO,111=JOI,100=I
using namespace std;
int n, ho[1202] = {}, memo[112][1001][112];
char a[1202];
int sh(int j, int day, int ij) {
  int A = 0;
  A = j - ho[day];
  if (A == 99 || A == 9 || A == 109 || A == 90 || A == 91 || A == -9 ||
      A == -99 || A == -90 || A == -89)
    return 0;
  int B;
  B = j - ij;
  if (ij == 11) {
    if (j == 110)
      B = 1000;
    if (j == 101)
      B = 1000;
  }
  if (ij == 110) {
    if (j == 11)
      B = 1000;
    if (j == 101)
      B = 1000;
  }
  if (ij == 101) {
    if (j == 110)
      B = 1000;
    if (j == 11)
      B = 1000;
  }

  if (B == 9 || B == 99 || B == 109 || B == -9 || B == 90 || B == 91 ||
      B == 89 || B == -99 || B == -90 || B == -89 || B == -109 || B == -91) {
    return 0;
  }
  if (day > n)
    return 0;

  if (day == n)
    return 1;

  int ans = 0;
  if (memo[j][day][ij] != -1) {
    return memo[j][day][ij];
  } else {

    ans += sh(1, day + 1, j);
    ans += sh(11, day + 1, j);
    ans += sh(10, day + 1, j);
    ans += sh(100, day + 1, j);
    ans += sh(110, day + 1, j);
    ans += sh(111, day + 1, j);
    ans += sh(101, day + 1, j);
  }
  ans %= 10007;
  memo[j][day][ij] = ans;
  return ans;
}
int main() {
  int s[2500] = {};
  cin >> n;
  cin >> a;
  ho[0] = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] == 'J')
      ho[i + 1] = 1;
    if (a[i] == 'O')
      ho[i + 1] = 10;
    if (a[i] == 'I')
      ho[i + 1] = 100;
  }
  for (int i = 0; i < 112; i++) {
    for (int g = 0; g < 1001; g++) {
      for (int jk = 0; jk < 112; jk++) {
        memo[i][g][jk] = -1;
      }
    }
  }

  cout << sh(1, 0, 0) << endl;

  return 0;
}

/*#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;

string moji,yoso[200];

int f(string a)
{
        if(moji.find(a,0))
        {

}
int main ()
{
        int z;
        int ans=0;
        cin>>z;
        cin>>moji;
        for(int i=0;i<z;i++)
        {
                cin>>yoso[i];

        }
        for(int i=0;i<z;i++)
        {
                if(moji.find(yoso[i],0)){ans++;continue;}
                f(yoso[i].at(0));
        }
        return 0;
}*/

Error: -11

Fixed submission:

#include <iostream>
// j=前の日,1=J,11=JO,111=JOI,100=I
using namespace std;
int n, ho[1002] = {}, memo[112][1001][112];
char a[1002];
int sh(int j, int day, int ij) {
  int A = 0;
  A = j - ho[day];
  if (A == 99 || A == 9 || A == 109 || A == 90 || A == 91 || A == -9 ||
      A == -99 || A == -90 || A == -89)
    return 0;
  int B;
  B = j - ij;
  if (ij == 11) {
    if (j == 110)
      B = 1000;
    if (j == 101)
      B = 1000;
  }
  if (ij == 110) {
    if (j == 11)
      B = 1000;
    if (j == 101)
      B = 1000;
  }
  if (ij == 101) {
    if (j == 110)
      B = 1000;
    if (j == 11)
      B = 1000;
  }

  if (B == 9 || B == 99 || B == 109 || B == -9 || B == 90 || B == 91 ||
      B == 89 || B == -99 || B == -90 || B == -89 || B == -109 || B == -91) {
    return 0;
  }
  if (day > n)
    return 0;

  if (day == n)
    return 1;

  int ans = 0;
  if (memo[j][day][ij] != -1) {
    return memo[j][day][ij];
  } else {

    ans += sh(1, day + 1, j);
    ans += sh(11, day + 1, j);
    ans += sh(10, day + 1, j);
    ans += sh(100, day + 1, j);
    ans += sh(110, day + 1, j);
    ans += sh(111, day + 1, j);
    ans += sh(101, day + 1, j);
  }
  ans %= 10007;
  memo[j][day][ij] = ans;
  return ans;
}
int main() {
  int s[2500] = {};
  cin >> n;
  cin >> a;
  ho[0] = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] == 'J')
      ho[i + 1] = 1;
    if (a[i] == 'O')
      ho[i + 1] = 10;
    if (a[i] == 'I')
      ho[i + 1] = 100;
  }
  for (int i = 0; i < 200; i++) {
    for (int g = 0; g < 1200; g++) {
      for (int jk = 0; jk < 200; jk++) {
        memo[i][g][jk] = -1;
      }
    }
  }

  cout << sh(1, 0, 0) << endl;

  return 0;
}

/*#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;

string moji,yoso[200];

int f(string a)
{
        if(moji.find(a,0))
        {

}
int main ()
{
        int z;
        int ans=0;
        cin>>z;
        cin>>moji;
        for(int i=0;i<z;i++)
        {
                cin>>yoso[i];

        }
        for(int i=0;i<z;i++)
        {
                if(moji.find(yoso[i],0)){ans++;continue;}
                f(yoso[i].at(0));
        }
        return 0;
}*/