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

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FRE freopen("in.txt", "r", stdin);
#define endl "\\n"
#define PI acos(-1.0)
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e5 + 10, M = 510;

bool flag;
char s[N][4];
int pre[N];
int n;

void dfs(int x, int a, int b, int c) {
  if (x > n) {
    flag = 1;
    return;
  }
  if (flag)
    return;
  char ch1 = s[x][0], ch2 = s[x][1];
  if (!strcmp(s[x], "AB")) {
    if (flag)
      return;
    if (b - 1 >= 0) {
      dfs(x + 1, a + 1, b - 1, c);
      pre[x] = 0;
    }
    if (flag)
      return;
    if (a - 1 >= 0) {
      dfs(x + 1, a - 1, b + 1, c);
      pre[x] = 1;
    }
  }
  if (!strcmp(s[x], "AC")) {
    if (flag)
      return;
    if (c - 1 >= 0) {
      dfs(x + 1, a + 1, b, c - 1);
      pre[x] = 0;
    }
    if (flag)
      return;
    if (a - 1 >= 0) {
      dfs(x + 1, a - 1, b, c + 1);
      pre[x] = 1;
    }
  }
  if (!strcmp(s[x], "BC")) {
    if (flag)
      return;
    if (c - 1 >= 0) {
      dfs(x + 1, a, b + 1, c - 1);
      pre[x] = 0;
    }
    if (flag)
      return;
    if (b - 1 >= 0) {
      dfs(x + 1, a, b - 1, c + 1);
      pre[x] = 1;
    }
  }
}

void solve() {
  int a, b, c;
  scanf("%d%d%d%d", &n, &a, &b, &c);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i]);
  dfs(1, a, b, c);
  if (flag) {
    puts("Yes");
    for (int i = 1; i <= n; i++)
      printf("%c\\n", s[i][pre[i]]);
  } else
    puts("No");
}

int main() {
  // FRE;
  // LL t; cin >> t; while(t--)
  solve();
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FRE freopen("in.txt", "r", stdin);
#define endl "\\n"
#define PI acos(-1.0)
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int N = 110, M = 510;

bool flag;
char s[N][4];
int pre[N];
int n;

void dfs(int x, int a, int b, int c) {
  if (x > n) {
    flag = 1;
    return;
  }
  if (flag)
    return;
  char ch1 = s[x][0], ch2 = s[x][1];
  if (!strcmp(s[x], "AB")) {
    if (flag)
      return;
    if (b - 1 >= 0) {
      dfs(x + 1, a + 1, b - 1, c);
      pre[x] = 0;
    }
    if (flag)
      return;
    if (a - 1 >= 0) {
      dfs(x + 1, a - 1, b + 1, c);
      pre[x] = 1;
    }
  } else if (!strcmp(s[x], "AC")) {
    if (flag)
      return;
    if (c - 1 >= 0) {
      dfs(x + 1, a + 1, b, c - 1);
      pre[x] = 0;
    }
    if (flag)
      return;
    if (a - 1 >= 0) {
      dfs(x + 1, a - 1, b, c + 1);
      pre[x] = 1;
    }
  } else if (!strcmp(s[x], "BC")) {
    if (flag)
      return;
    if (c - 1 >= 0) {
      dfs(x + 1, a, b + 1, c - 1);
      if (flag)
        return;
      pre[x] = 0;
    }
    if (flag)
      return;
    if (b - 1 >= 0) {
      dfs(x + 1, a, b - 1, c + 1);
      if (flag)
        return;
      pre[x] = 1;
    }
  }
}

void solve() {
  int a, b, c;
  scanf("%d%d%d%d", &n, &a, &b, &c);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i]);
  dfs(1, a, b, c);
  if (flag) {
    puts("Yes");
    for (int i = 1; i <= n; i++) {
      printf("%c\\n", s[i][pre[i]]);
    }
  } else {
    puts("No");
  }
}

int main() {
  // FRE;
  // LL t; cin >> t; while(t--)
  solve();
  return 0;
}