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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
#define pb push_back
#define FAST_IO                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> input) {
  os << "(" << input.first << ", " << input.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> input) {
  for (auto t : input)
    os << t << " ";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> input) {
  for (auto t : input)
    os << t << " ";
  return os;
}

string s[100002];

vector<char> ans;

int arr[3];
int n;
void solveDeterministic() {
  for (int i = 1; i <= n; i++) {
    string ss = s[i];
    int moves = 0;
    for (char c : ss) {
      int in = c - 'A';
      if (arr[in] > 0) {
        moves++;
      }
    }
    if (moves == 1) {
      char x = ss[0];
      char y = ss[1];
      if (arr[x - 'A'] <= 0) {
        swap(x, y);
        assert(arr[x - 'A'] > 0);
      }
      ans.push_back(y);
      arr[y - 'A']++;
      arr[x - 'A']--;
      // cout << arr[0] << " " << arr[1] << " " <<arr[2] << "\\n";
      assert(arr[0] >= 0 && arr[1] >= 0 && arr[2] >= 0);

    } else if (moves == 0) {
      cout << "No\\n";
      exit(0);
    } else {
      return;
    }
  }
}

int lastGroup[100002];

char different(string a, string b) {
  assert(a != b);
  if (a[0] == b[0])
    return a[1];
  if (a[0] == b[1])
    return a[1];
  return a[0];
}
int main() {
  cin >> n;
  cin >> arr[0] >> arr[1] >> arr[2];
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  solveDeterministic();
  int i = ans.size();
  if (i == n) {
    cout << "Yes\\n";
    for (char c : ans)
      cout << c << "\\n";
  } else {
    lastGroup[n] = n;
    for (int i = n - 1; i >= 1; i--) {
      if (s[i] == s[i + 1]) {
        lastGroup[i] = lastGroup[i + 1];
      } else {
        lastGroup[i] = i;
      }
    }
    int i = ans.size();
    i++;
    while (i <= n) {
      int d = lastGroup[i];
      char diffChar = s[i][0];
      if (d + 1 <= n)
        diffChar = different(s[i], s[d + 1]);
      char sameChar = (s[i][0] == diffChar) ? s[i][1] : s[i][0];
      if ((d - i) % 2 != 0) {
        swap(sameChar, diffChar);
      }

      if (arr[diffChar - 'A'] == 0)
        swap(sameChar, diffChar);

      arr[sameChar - 'A']++;
      arr[diffChar - 'A']--;
      // cout << arr[0] << " " <<arr[1] << " " << arr[2] << "\\n";
      // assert(arr[0] >=0 && arr[1] >= 0 && arr[2] >= 0);
      ans.push_back(sameChar);
      i++;
    }
    cout << "Yes\\n";
    for (char c : ans)
      cout << c << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
#define pb push_back
#define FAST_IO                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> input) {
  os << "(" << input.first << ", " << input.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> input) {
  for (auto t : input)
    os << t << " ";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> input) {
  for (auto t : input)
    os << t << " ";
  return os;
}

string s[100002];

vector<char> ans;

int arr[3];
int n;
void solveDeterministic() {
  for (int i = 1; i <= n; i++) {
    string ss = s[i];
    int moves = 0;
    for (char c : ss) {
      int in = c - 'A';
      if (arr[in] > 0) {
        moves++;
      }
    }
    if (moves == 1) {
      char x = ss[0];
      char y = ss[1];
      if (arr[x - 'A'] <= 0) {
        swap(x, y);
        assert(arr[x - 'A'] > 0);
      }
      ans.push_back(y);
      arr[y - 'A']++;
      arr[x - 'A']--;
      // cout << arr[0] << " " << arr[1] << " " <<arr[2] << "\\n";
      assert(arr[0] >= 0 && arr[1] >= 0 && arr[2] >= 0);

    } else if (moves == 0) {
      cout << "No\\n";
      exit(0);
    } else {
      return;
    }
  }
}

int lastGroup[100002];

char different(string a, string b) {
  assert(a != b);
  if (a[0] == b[0])
    return a[1];
  if (a[0] == b[1])
    return a[1];
  return a[0];
}
int main() {
  cin >> n;
  cin >> arr[0] >> arr[1] >> arr[2];
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  solveDeterministic();
  int i = ans.size();
  if (i == n) {
    cout << "Yes\\n";
    for (char c : ans)
      cout << c << "\\n";
  } else {
    lastGroup[n] = n;
    for (int i = n - 1; i >= 1; i--) {
      if (s[i] == s[i + 1]) {
        lastGroup[i] = lastGroup[i + 1];
      } else {
        lastGroup[i] = i;
      }
    }
    int i = ans.size();
    i++;
    while (i <= n) {
      int d = lastGroup[i];
      char diffChar = s[i][0];
      if (d + 1 <= n)
        diffChar = different(s[i], s[d + 1]);
      char sameChar = (s[i][0] == diffChar) ? s[i][1] : s[i][0];
      if ((d - i) % 2 != 0) {
        swap(sameChar, diffChar);
      }
      if (i == n) {
        if (arr[diffChar - 'A'] == 0)
          swap(sameChar, diffChar);
      }
      arr[sameChar - 'A']++;
      arr[diffChar - 'A']--;
      // cout << arr[0] << " " <<arr[1] << " " << arr[2] << "\\n";
      assert(arr[0] >= 0 && arr[1] >= 0 && arr[2] >= 0);
      ans.push_back(sameChar);
      i++;
    }
    cout << "Yes\\n";
    for (char c : ans)
      cout << c << "\\n";
  }
  return 0;
}