Problem p03018 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using pll = pair<ll, ll>;
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FI first
#define SE second
#define endl "\n"
#define ciosup                                                                 \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false);
#define eb emplace_back
constexpr ll INF = 1e9 + 7;
constexpr ll MOD = 1e9 + 7;

int main() {
  ciosup;
  string s[200'000];
  int j = 0;
  char moji, moji2;

  while (cin >> moji) {
    if (moji == 'A') {
      s[j].append("X");
    } else if (moji == 'B') {
      if (cin >> moji2) {
        // cout << "MOJI2 " << moji2 << endl;
        if (moji2 == 'C') {
          s[j].append("Y");
        } else if (moji2 == 'A') {
          j++;
          s[j].append("X");
        } else if (moji2 == 'B') {
          j++;
        }
      }
    } else if (moji == 'C') {
      j++;
    }
  }
  if (s[j].size() == 0)
    j--;
  ll ans = 0;
  REP(i, j + 1) {
    int ypos = 0;
    REP(k, s[i].size()) {
      if (s[i][k] == 'Y') {
        ans += k - ypos;
        ypos++;
      }
    }
  }
  // REP(i,j+1)
  // cout << s[i] << endl;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using pll = pair<ll, ll>;
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FI first
#define SE second
#define endl "\n"
#define ciosup                                                                 \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false);
#define eb emplace_back
constexpr ll INF = 1e9 + 7;
constexpr ll MOD = 1e9 + 7;

int main() {
  ciosup;
  string s[400'000];
  int j = 0;
  char moji, moji2;

  while (cin >> moji) {
    if (moji == 'A') {
      s[j].append("X");
    } else if (moji == 'B') {
      if (cin >> moji2) {
        // cout << "MOJI2 " << moji2 << endl;
        if (moji2 == 'C') {
          s[j].append("Y");
        } else if (moji2 == 'A') {
          j++;
          s[j].append("X");
        } else if (moji2 == 'B') {
          j++;
        }
      }
    } else if (moji == 'C') {
      j++;
    }
  }
  if (s[j].size() == 0)
    j--;
  ll ans = 0;
  REP(i, j + 1) {
    int ypos = 0;
    REP(k, s[i].size()) {
      if (s[i][k] == 'Y') {
        ans += k - ypos;
        ypos++;
      }
    }
  }
  // REP(i,j+1)
  // cout << s[i] << endl;
  cout << ans << endl;
}