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

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

int N, A, B, C;
string moves[100005];

string ans = "";
void move(int i) {
  if (moves[i] == "AB") {
    if (A < B) {
      A++;
      B--;

      ans += "A\\n";
    }

    else if (B < A) {
      B++;
      A--;

      ans += "B\\n";
    }

    else {
      if (A == 1 && B == 1) {
        if (C == 0 && moves[i + 1] == "AC") {
          A++;
          B--;

          ans += "A\\n";
        }

        else {
          A--;
          B++;

          ans += "B\\n";
        }
      }

      else {
        A++;
        B--;

        ans += "A\\n";
      }
    }

    if (A < 0 || B < 0 || C < 0) {
      printf("No");
      exit(0);
    }
  }

  else if (moves[i] == "BC") {
    if (B < C) {
      B++;
      C--;

      ans += "B\\n";
    }

    else if (C < B) {
      C++;
      B--;

      ans += "C\\n";
    }

    else {
      if (B == 1 && C == 1) {
        if (A == 0 && moves[i + 1] == "AB") {
          B++;
          C--;

          ans += "B\\n";
        }

        else {
          C++;
          B--;

          ans += "C\\n";
        }
      }

      else {
        B++;
        C--;

        ans += "B\\n";
      }
    }

    if (A < 0 || B < 0 || C < 0) {
      printf("No");
      exit(0);
    }
  }

  else {
    if (A < C) {
      A++;
      C--;

      ans += "A\\n";
    }

    else if (C < A) {
      C++;
      A--;

      ans += "C\\n";
    }

    else {
      if (A == 1 && C == 1) {
        if (B == 0 && moves[i + 1] == "BC") {
          C++;
          A--;

          ans += "C\\n";
        }

        else {
          A++;
          C--;

          ans += "A\\n";
        }
      }

      else {
        A++;
        C--;

        ans += "A\\n";
      }
    }

    if (A < 0 || B < 0 || C < 0) {
      printf("No");
      exit(0);
    }
  }
}

int main() {
  scanf("%d%d%d%d", &N, &A, &B, &C);

  for (int i = 1; i <= N; i++) {
    cin >> moves[i];
  }

  for (int i = 1; i <= N; i++) {
    move(i);
  }

  printf("Yes\\n");
  printf("%s", ans.c_str());
}

Error: -11

Fixed submission:

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

int N, A, B, C;

vector<string> queries;
string ans = "";

int aCount[100005], bCount[100005], cCount[100005];

int main() {
  scanf("%d%d%d%d", &N, &A, &B, &C);

  int i;
  for (i = 1; i <= N; i++) {
    string s;
    cin >> s;

    queries.push_back(s);
  }

  for (i = N; i >= 1; i--) {
    if (queries[i][0] == 'A' || queries[i][1] == 'A')
      aCount[i] = aCount[i + 1] + 1;

    if (queries[i][0] == 'B' || queries[i][1] == 'B')
      bCount[i] = bCount[i + 1] + 1;

    if (queries[i][0] == 'C' || queries[i][1] == 'C')
      cCount[i] = cCount[i + 1] + 1;
  }

  for (i = 1; i <= N; i++) {
    string s = queries[i];
    if (s == "AB") {
      if (A < B) {
        A++;
        B--;

        ans += 'A';
      }

      else if (B < A) {
        A--;
        B++;

        ans += 'B';
      }

      else {
        if (aCount[i + 1] < bCount[i + 1]) {
          A--;
          B++;

          ans += 'B';
        }

        else {
          B--;
          A++;

          ans += 'A';
        }
      }

      if (A < 0 || B < 0) {
        printf("No");
        return 0;
      }
    }

    else if (s == "BC") {
      if (B < C) {
        B++;
        C--;

        ans += 'B';
      }

      else if (C < B) {
        B--;
        C++;

        ans += 'C';
      }

      else {
        if (bCount[i + 1] < cCount[i + 1]) {
          B--;
          C++;

          ans += 'C';
        }

        else {
          B++;
          C--;

          ans += 'B';
        }
      }

      if (B < 0 || C < 0) {
        printf("No");
        return 0;
      }
    }

    else {
      if (A < C) {
        A++;
        C--;

        ans += 'A';
      }

      else if (C < A) {
        A--;
        C++;

        ans += 'C';
      }

      else {
        if (aCount[i + 1] < cCount[i + 1]) {
          A--;
          C++;

          ans += 'C';
        }

        else {
          C--;
          A++;

          ans += 'A';
        }
      }

      if (A < 0 || C < 0) {
        printf("No");
        return 0;
      }
    }
  }
}