Problem p03998 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define cinsc                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(0);
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOR(i, k, n) for (int64 i = k; i < n; i++)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define SQ(x) ((x) * (x))
const double pi = acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;

int64 powr(int64 x, int64 y, int64 p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

void in_o() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cinsc;
}

int main() {
  in_o();
  string s[3];
  cin >> s[0] >> s[1] >> s[2];
  int top = 0, next, ans;
  while (1) {
    if (s[top].empty()) {
      ans = top;
      break;
    }
    if (s[top][0] == 'a')
      next = 0;
    else if (s[top][0] == 'b')
      next = 1;
    else
      next = 2;
    s[top] = s[top].substr(1);
    top = next;
  }
  if (ans == 0)
    cout << "A" << endl;
  else if (ans == 1)
    cout << "B" << endl;
  else
    cout << "C" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define cinsc                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(0);
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOR(i, k, n) for (int64 i = k; i < n; i++)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define SQ(x) ((x) * (x))
const double pi = acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;

int64 powr(int64 x, int64 y, int64 p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

void in_o() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cinsc;
}

int main() {
  // in_o();
  string s[3];
  cin >> s[0] >> s[1] >> s[2];
  int top = 0, next, ans;
  while (1) {
    if (s[top].empty()) {
      ans = top;
      break;
    }
    if (s[top][0] == 'a')
      next = 0;
    else if (s[top][0] == 'b')
      next = 1;
    else
      next = 2;
    s[top] = s[top].substr(1);
    top = next;
  }
  if (ans == 0)
    cout << "A" << endl;
  else if (ans == 1)
    cout << "B" << endl;
  else
    cout << "C" << endl;
  return 0;
}