Problem p03533 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REPR(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase(unique(ALL(v)), v.end());

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = (1 << 28);
const ll LINF = 2e18;
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  vector<char> Must({'K', 'I', 'H', 'B', 'R'});
  bool ok = true;
  int ind = 0;
  REP(i, SZ(S)) {
    if (S[i] == 'A') {
      if (ok) {
        ok = false;
      } else {
        cout << "NO" << endl;
        return 0;
      }
    } else {
      if (S[i] != Must[ind]) {
        cout << "NO" << endl;
        return 0;
      }
      ind++;
      if (ind >= 3) {
        ok = true;
      } else {
        ok = false;
      }
    }
    if (ind == 5) {
      bool ans = false;
      if (i == SZ(S) - 1)
        ans = true;
      if (i == SZ(S) - 2 & S[SZ(S) - 1] == 'A') {
        ans = true;
      }
      if (ans) {
        cout << "YES" << endl;
        return 0;
      } else {
        cout << "NO" << endl;
        return 0;
      }
    }
  }
  assert(false);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REPR(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase(unique(ALL(v)), v.end());

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = (1 << 28);
const ll LINF = 2e18;
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  vector<char> Must({'K', 'I', 'H', 'B', 'R'});
  bool ok = true;
  int ind = 0;
  REP(i, SZ(S)) {
    if (S[i] == 'A') {
      if (ok) {
        ok = false;
      } else {
        cout << "NO" << endl;
        return 0;
      }
    } else {
      if (S[i] != Must[ind]) {
        cout << "NO" << endl;
        return 0;
      }
      ind++;
      if (ind >= 3) {
        ok = true;
      } else {
        ok = false;
      }
    }
    if (ind == 5) {
      bool ans = false;
      if (i == SZ(S) - 1)
        ans = true;
      if (i == SZ(S) - 2 & S[SZ(S) - 1] == 'A') {
        ans = true;
      }
      if (ans) {
        cout << "YES" << endl;
        return 0;
      } else {
        cout << "NO" << endl;
        return 0;
      }
    }
  }
  cout << "NO" << endl;
  return 0;
}