Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 4e3 + 7;
char s[N];
int a[N];
int b[N];
int c[N];
int main() {
  memset(a, 0, sizeof a);
  memset(b, 0, sizeof b);
  memset(c, 0, sizeof c);
  int n;
  ll ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> s[i];
  for (int i = n; i >= 1; i--) {
    a[i] += a[i + 1] + (s[i] == 'R');
    b[i] += b[i + 1] + (s[i] == 'G');
    c[i] += c[i + 1] + (s[i] == 'B');
  }
  //    for(int i = 1; i <= n; i++)
  //        cout << a[i] << " ";
  //    cout << endl;
  //    for(int i = 1; i <= n; i++)
  //        cout << b[i] << " ";
  //    cout << endl;
  //    for(int i = 1; i <= n; i++)
  //        cout << c[i] << " ";
  //    cout << endl;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'R') {
      for (int j = i + 1; j <= n; j++) {
        if (s[j] == 'G') {
          ans += c[j + 1];
          if (s[j * 2 - i] == 'B' && j * 2 - i <= n)
            ans--;
        } else if (s[j] == 'B') {
          ans += b[j + 1];
          if (s[j * 2 - i] == 'G' && j * 2 - i <= n)
            ans--;
        }
      }
    }
    if (s[i] == 'G') {
      for (int j = i + 1; j <= n; j++) {
        if (s[j] == 'R') {
          ans += c[j + 1];
          if (s[j * 2 - i] == 'B' && j * 2 - i <= n)
            ans--;
        } else if (s[j] == 'B') {
          ans += a[j + 1];
          if (s[j * 2 - i] == 'R' && j * 2 - i <= n)
            ans--;
        }
      }
    }
    if (s[i] == 'B') {
      for (int j = i + 1; j <= n; j++) {
        if (s[j] == 'R') {
          ans += b[j + 1];
          if (s[j * 2 - i] == 'G' && j * 2 - i <= n)
            ans--;
        } else if (s[j] == 'G') {
          ans += a[j + 1];
          if (s[j * 2 - i] == 'R' && j * 2 - i <= n)
            ans--;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 1e4 + 7;
char s[N];
int a[N];
int b[N];
int c[N];
int main() {
  memset(a, 0, sizeof a);
  memset(b, 0, sizeof b);
  memset(c, 0, sizeof c);
  int n;
  ll ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> s[i];
  for (int i = n; i >= 1; i--) {
    a[i] += a[i + 1] + (s[i] == 'R');
    b[i] += b[i + 1] + (s[i] == 'G');
    c[i] += c[i + 1] + (s[i] == 'B');
  }
  //    for(int i = 1; i <= n; i++)
  //        cout << a[i] << " ";
  //    cout << endl;
  //    for(int i = 1; i <= n; i++)
  //        cout << b[i] << " ";
  //    cout << endl;
  //    for(int i = 1; i <= n; i++)
  //        cout << c[i] << " ";
  //    cout << endl;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'R') {
      for (int j = i + 1; j <= n; j++) {
        if (s[j] == 'G') {
          ans += c[j + 1];
          if (s[j * 2 - i] == 'B' && j * 2 - i <= n)
            ans--;
        } else if (s[j] == 'B') {
          ans += b[j + 1];
          if (s[j * 2 - i] == 'G' && j * 2 - i <= n)
            ans--;
        }
      }
    }
    if (s[i] == 'G') {
      for (int j = i + 1; j <= n; j++) {
        if (s[j] == 'R') {
          ans += c[j + 1];
          if (s[j * 2 - i] == 'B' && j * 2 - i <= n)
            ans--;
        } else if (s[j] == 'B') {
          ans += a[j + 1];
          if (s[j * 2 - i] == 'R' && j * 2 - i <= n)
            ans--;
        }
      }
    }
    if (s[i] == 'B') {
      for (int j = i + 1; j <= n; j++) {
        if (s[j] == 'R') {
          ans += b[j + 1];
          if (s[j * 2 - i] == 'G' && j * 2 - i <= n)
            ans--;
        } else if (s[j] == 'G') {
          ans += a[j + 1];
          if (s[j * 2 - i] == 'R' && j * 2 - i <= n)
            ans--;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}