Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int N = 2e5 + 5;
const int Mod = 1e9 + 7;
#define pi acos(-1)
#define INF 1e9 + 5
#define INM INT_MIN
#define pb(a) push_back(a)
#define mk(a, b) make_pair(a, b)
#define dbg(x) cout << "now this num is " << x << endl;
#define met0(axx) memset(axx, 0, sizeof(axx));
#define metf(axx) memset(axx, -1, sizeof(axx));
#define sd(ax) scanf("%d", &ax)
#define sld(ax) scanf("%lld", &ax)
#define sldd(ax, bx) scanf("%lld %lld", &ax, &bx)
#define sdd(ax, bx) scanf("%d %d", &ax, &bx)
#define sddd(ax, bx, cx) scanf("%d %d %d", &ax, &bx, &cx)
#define sfd(ax) scanf("%lf", &ax)
#define sfdd(ax, bx) scanf("%lf %lf", &ax, &bx)
#define pr(a) printf("%d\n", a)
#define plr(a) printf("%lld\n", a)
/*
贪心？pu..看zzl学长的贪心,emm..很微妙.也能过
但是可以直接dfs暴搜..
*/
int n, f = 0;
string s[N], ans;
void dfs(int x, int y, int z, int i) {
  if (f)
    return;
  if (i == n + 1) {
    if (x >= 0 && y >= 0 && z >= 0) {
      f = 1;
      printf("Yes\n");
      for (int i = 1; i <= n; ++i)
        cout << ans[i] << "\n";
    }
    return;
  }
  if (s[i] == "AB") {
    if (y > 0)
      ans[i] = 'A', dfs(x + 1, y - 1, z, i + 1);
    if (x > 0)
      ans[i] = 'B', dfs(x - 1, y + 1, z, i + 1);
  }
  if (s[i] == "BC") {
    if (z > 0)
      ans[i] = 'B', dfs(x, y + 1, z - 1, i + 1);
    if (y > 0)
      ans[i] = 'C', dfs(x, y - 1, z + 1, i + 1);
  }
  if (s[i] == "AC") {
    if (z > 0)
      ans[i] = 'A', dfs(x + 1, y, z - 1, i + 1);
    if (x > 0)
      ans[i] = 'C', dfs(x - 1, y, z + 1, i + 1);
  }
}
int main() {
  sd(n);
  int a, b, c;
  sddd(a, b, c);
  for (int i = 1; i <= n; ++i)
    cin >> s[i];
  dfs(a, b, c, 1);
  if (!f)
    printf("No\n");
  //	system("pause");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int N = 2e5 + 5;
const int Mod = 1e9 + 7;
#define pi acos(-1)
#define INF 1e9 + 5
#define INM INT_MIN
#define pb(a) push_back(a)
#define mk(a, b) make_pair(a, b)
#define dbg(x) cout << "now this num is " << x << endl;
#define met0(axx) memset(axx, 0, sizeof(axx));
#define metf(axx) memset(axx, -1, sizeof(axx));
#define sd(ax) scanf("%d", &ax)
#define sld(ax) scanf("%lld", &ax)
#define sldd(ax, bx) scanf("%lld %lld", &ax, &bx)
#define sdd(ax, bx) scanf("%d %d", &ax, &bx)
#define sddd(ax, bx, cx) scanf("%d %d %d", &ax, &bx, &cx)
#define sfd(ax) scanf("%lf", &ax)
#define sfdd(ax, bx) scanf("%lf %lf", &ax, &bx)
#define pr(a) printf("%d\n", a)
#define plr(a) printf("%lld\n", a)
/*
贪心？pu..看zzl学长的贪心,emm..很微妙.也能过
但是可以直接dfs暴搜..
*/
int n, f = 0;
string s[N];
char ans[N];
void dfs(int x, int y, int z, int i) {
  if (f)
    return;
  if (i == n + 1) {
    if (x >= 0 && y >= 0 && z >= 0) {
      f = 1;
      printf("Yes\n");
      for (int i = 1; i <= n; ++i)
        cout << ans[i] << "\n";
    }
    return;
  }
  if (s[i] == "AB") {
    if (y > 0)
      ans[i] = 'A', dfs(x + 1, y - 1, z, i + 1);
    if (x > 0)
      ans[i] = 'B', dfs(x - 1, y + 1, z, i + 1);
  }
  if (s[i] == "BC") {
    if (z > 0)
      ans[i] = 'B', dfs(x, y + 1, z - 1, i + 1);
    if (y > 0)
      ans[i] = 'C', dfs(x, y - 1, z + 1, i + 1);
  }
  if (s[i] == "AC") {
    if (z > 0)
      ans[i] = 'A', dfs(x + 1, y, z - 1, i + 1);
    if (x > 0)
      ans[i] = 'C', dfs(x - 1, y, z + 1, i + 1);
  }
}
int main() {
  sd(n);
  int a, b, c;
  sddd(a, b, c);
  for (int i = 1; i <= n; ++i)
    cin >> s[i];
  dfs(a, b, c, 1);
  if (!f)
    printf("No\n");
  //	system("pause");
  return 0;
}