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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int N = 100000 + 100;
const db pi = acos(-1.0);
#define lowbit(x) (x) & (-x)
#define sqr(x) (x) * (x)
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define per(i, a, b) for (register int i = a; i >= b; i--)
#define go(u, i)                                                               \\
  for (register int i = head[u], v = sq[i].to; i; i = sq[i].nxt, v = sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

inline ll readll() {
  ll x = 0;
  int f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

int n, a[100100], b[5], m;
vector<pair<string, int>> ask;
string s[100100], ans = "";

int work(string s, string t) {
  rep(i, 0, 1) rep(j, 0, 1) if (s[i] == t[j]) return (s[i] - 'A');
}

int main() {
  n = read();
  rep(i, 0, 2) a[i] = read();
  rep(i, 1, n) cin >> s[i];
  int c = 1;
  // cout << work("BC","AB") << endl;
  rep(i, 1, n) {
    if ((i == n) || (s[i] != s[i + 1])) {
      ask.pb(mkp(s[i], c));
      c = 1;
    } else
      c++;
  }
  m = ask.size();
  int now = 0;
  rep(i, 0, m - 1) {
    string op = ask[i].fir;
    int c = ask[i].sec;
    assert(c > 0);
    now += c;
    int x = op[0] - 'A', y = op[1] - 'A';
    if ((!a[x]) && (!a[y])) {
      puts("No");
      return 0;
    }
    if (a[x] < a[y])
      swap(x, y);
    // cout << x << " " << y << endl;
    rep(j, 1, c) if (j & 1) ans = ans + (char)('A' + y);
    else ans = ans + (char)('A' + x);
    if (c & 1) {
      a[x]--;
      a[y]++;
    }
    if (i + 1 < m) {
      string nxtop = ask[i + 1].fir;
      int p = work(nxtop, op), q = x + y - p, r = 3 - x - y;
      if ((!a[r]) && (!a[p])) {
        // cout << 'a' << endl;
        int tmp = now - 1;
        while (ans[tmp] == ('A' + p))
          tmp--;
        a[p]++;
        a[q]--;
        ans[tmp] = (char)('A' + p);
        a[p]++;
        a[q]--;
        if (a[q] < 0) {
          puts("No");
          return 0;
        }
      }
    }
  }
  puts("Yes");
  rep(i, 0, n - 1) {
    putchar(ans[i]);
    puts("");
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int N = 100000 + 100;
const db pi = acos(-1.0);
#define lowbit(x) (x) & (-x)
#define sqr(x) (x) * (x)
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define per(i, a, b) for (register int i = a; i >= b; i--)
#define go(u, i)                                                               \\
  for (register int i = head[u], v = sq[i].to; i; i = sq[i].nxt, v = sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

inline ll readll() {
  ll x = 0;
  int f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

int n, a[100100], b[5], m;
vector<pair<string, int>> ask;
string s[100100], ans = "";

int work(string s, string t) {
  rep(i, 0, 1) rep(j, 0, 1) if (s[i] == t[j]) return (s[i] - 'A');
}

int main() {
  n = read();
  rep(i, 0, 2) a[i] = read();
  rep(i, 1, n) cin >> s[i];
  int c = 1;
  // cout << work("BC","AB") << endl;
  rep(i, 1, n) {
    if ((i == n) || (s[i] != s[i + 1])) {
      ask.pb(mkp(s[i], c));
      c = 1;
    } else
      c++;
  }
  m = ask.size();
  rep(i, 0, m - 1) {
    string op = ask[i].fir;
    int c = ask[i].sec;
    assert(c > 0);
    int x = op[0] - 'A', y = op[1] - 'A';
    if ((!a[x]) && (!a[y])) {
      puts("No");
      return 0;
    } else if ((a[x] == 1) && (a[y] == 1) && (c & 1) && (i + 1 < m)) {
      return -1;
      string nxtop = ask[i + 1].fir;
      int s = work(nxtop, op), t;
      if (s == x)
        t = y;
      else
        t = x;
      // cout << s << " " << t << endl;
      rep(j, 1, c) {
        if (j & 1)
          ans = ans + (char)('A' + s);
        else
          ans = ans + (char)('A' + t);
      }
      if (c & 1) {
        a[s]++;
        a[t]--;
      }
    } else {
      if (a[x] < a[y])
        swap(x, y);
      rep(j, 1, c) if (j & 1) ans = ans + (char)('A' + y);
      else ans = ans + (char)('A' + x);
      if (c & 1) {
        a[x]--;
        a[y]++;
      }
    }
  }
  puts("Yes");
  rep(i, 0, n - 1) {
    putchar(ans[i]);
    puts("");
  }
  return 0;
}