Problem:
Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A

Solution in Rust (1.42.0) (judge verdict: Accepted)

#[macro_use]
mod input_mcr {
    // ref: tanakh <https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8>
    // diff: using Parser
    #[macro_export(local_inner_macros)]
    macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut parser = Parser::from_str($s);
        input_inner!{parser, $($r)*}
    };
    (parser = $parser:ident, $($r:tt)*) => {
        input_inner!{$parser, $($r)*}
    };
    (new_stdin_parser = $parser:ident, $($r:tt)*) => {
        let stdin = std::io::stdin();
        let reader = std::io::BufReader::new(stdin.lock());
        let mut $parser = Parser::new(reader);
        input_inner!{$parser, $($r)*}
    };
    ($($r:tt)*) => {
        input!{new_stdin_parser = parser, $($r)*}
    };
}

    #[macro_export(local_inner_macros)]
    macro_rules! input_inner {
    ($parser:ident) => {};
    ($parser:ident, ) => {};
    ($parser:ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($parser, $t);
        input_inner!{$parser $($r)*}
    };
}

    #[macro_export(local_inner_macros)]
    macro_rules! read_value {
    ($parser:ident, ( $($t:tt),* )) => {
        ( $(read_value!($parser, $t)),* )
    };
    ($parser:ident, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($parser, $t)).collect::<Vec<_>>()
    };
    ($parser:ident, chars) => {
        read_value!($parser, String).chars().collect::<Vec<char>>()
    };
    ($parser:ident, char_) => {
        read_value!($parser, String).chars().collect::<Vec<char>>()[0]
    };
    ($parser:ident, usize1) => {
        read_value!($parser, usize) - 1
    };
    ($parser:ident, line) => {
        $parser.next_line()
    };
    ($parser:ident, line_) => {
        $parser.next_line().chars().collect::<Vec<char>>()
    };
    ($parser:ident, i64_) => {
        $parser.fast_i64()
    };
    ($parser:ident, usize_) => {
        $parser.fast_i64() as usize
    };
    ($parser:ident, usize1_) => {
        ($parser.fast_i64() - 1) as usize
    };
    ($parser:ident, $t:ty) => {
        $parser.next::<$t>().expect("Parse error")
    };
}

    use std::io;
    use std::io::BufRead;
    use std::str;

    // ref: tatsuya6502 <https://qiita.com/tatsuya6502/items/cd448486f7ef7b5b8c7e>
    // ref: wariuni <https://qiita.com/tatsuya6502/items/cd448486f7ef7b5b8c7e#comment-7040a5ae96305e884eb9>
    // diff: using std::io::BufRead::fill_buf()
    pub struct Parser<R> {
        pub reader: R,
        buf: Vec<u8>,
        pos: usize,
    }

    impl Parser<io::Empty> {
        pub fn from_str(s: &str) -> Parser<io::Empty> {
            Parser {
                reader: io::empty(),
                buf: s.as_bytes().to_vec(),
                pos: 0,
            }
        }
    }

    impl<R: BufRead> Parser<R> {
        pub fn new(reader: R) -> Parser<R> {
            Parser {
                reader: reader,
                buf: vec![],
                pos: 0,
            }
        }
        pub fn update_buf(&mut self) {
            self.buf.clear();
            self.pos = 0;
            loop {
                let (len, complete) = {
                    let buf2 = self.reader.fill_buf().unwrap();
                    self.buf.extend_from_slice(buf2);
                    let len = buf2.len();
                    (len, buf2[len - 1] <= 0x20)
                };
                self.reader.consume(len);
                if complete {
                    break;
                }
            }
        }
        pub fn next<T: str::FromStr>(&mut self) -> Result<T, T::Err> {
            loop {
                let mut begin = self.pos;
                while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                    begin += 1;
                }
                let mut end = begin;
                while end < self.buf.len() && (self.buf[end] > 0x20) {
                    end += 1;
                }
                if begin != self.buf.len() {
                    self.pos = end;
                    return unsafe { str::from_utf8_unchecked(&self.buf[begin..end]) }.parse::<T>();
                } else {
                    self.update_buf();
                }
            }
        }
        pub fn next_line(&mut self) -> String {
            loop {
                let mut begin = self.pos;
                if begin < self.buf.len() && (self.buf[begin] < 0x20) {
                    begin += 1;
                }
                let mut end = begin;
                while end < self.buf.len() && (self.buf[end] >= 0x20) {
                    end += 1;
                }
                if begin != self.buf.len() {
                    self.pos = end;
                    return unsafe { str::from_utf8_unchecked(&self.buf[begin..end]) }.to_string();
                } else {
                    self.update_buf();
                }
            }
        }
        pub fn fast_i64(&mut self) -> i64 {
            loop {
                let mut begin = self.pos;
                while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                    begin += 1;
                }
                if begin == self.buf.len() {
                    self.update_buf();
                    continue;
                }
                let mut res = 0;
                let (is_positive, mut end) = match self.buf[begin] {
                    b'+' => (true, begin + 1),
                    b'-' => (false, begin + 1),
                    _ => (true, begin),
                };
                while end < self.buf.len() && (self.buf[end] > 0x20) {
                    res = res * 10 + (self.buf[end] as i64 - '0' as i64);
                    end += 1;
                }
                if begin != self.buf.len() {
                    self.pos = end;
                    return if is_positive { res } else { -res };
                } else {
                    self.update_buf();
                }
            }
        }
    }
}

use input_mcr::*;
use std::cmp::*;

fn dfs(ss: &Vec<String>, i: usize, a: i64, b: i64, c: i64, hist: &mut Vec<i64>) {
    if i == ss.len() {
        println!("Yes");
        for h in hist {
            match h {
                0 => println!("A"),
                1 => println!("B"),
                2 => println!("C"),
                _ => panic!(),
            }
        }
        std::process::exit(0);
    }
    if ss[i] == "AB" {
        if b >= 1 {
            hist.push(0);
            dfs(ss, i + 1, a + 1, b - 1, c, hist);
            hist.pop();
        }
        if a >= 1 {
            hist.push(1);
            dfs(ss, i + 1, a - 1, b + 1, c, hist);
            hist.pop();
        }
    }
    if ss[i] == "BC" {
        if c >= 1 {
            hist.push(1);
            dfs(ss, i + 1, a, b + 1, c - 1, hist);
            hist.pop();
        }
        if b >= 1 {
            hist.push(2);
            dfs(ss, i + 1, a, b - 1, c + 1, hist);
            hist.pop();
        }
    }
    if ss[i] == "AC" {
        if c >= 1 {
            hist.push(0);
            dfs(ss, i + 1, a + 1, b, c - 1, hist);
            hist.pop();
        }
        if a >= 1 {
            hist.push(2);
            dfs(ss, i + 1, a - 1, b, c + 1, hist);
            hist.pop();
        }
    }
}

fn step(al: i64, ah: i64, bl: i64, bh: i64) -> (i64, i64, i64, i64) {
    let inf = 1i64 << 50;
    let (mut al2, mut ah2, mut bl2, mut bh2) = (inf, -inf, inf, -inf);
    // reduce A
    // max(0,al-1), ah-1 bl+1, bh+1
    if ah >= 1 {
        al2 = min(al2, max(0, al - 1));
        ah2 = max(ah2, max(0, ah - 1));
        bl2 = min(bl2, bl + 1);
        bh2 = max(bh2, bh + 1);
    }
    // reduce B
    // al+1,ah+1,bl-1,bh-1
    if bh >= 1 {
        al2 = min(al2, al + 1);
        ah2 = max(ah2, ah + 1);
        bl2 = min(bl2, max(0, bl - 1));
        bh2 = max(bh2, max(0, bh - 1));
    }
    (al2 + (ah2 - al2) % 2, ah2, bl2 + (bh2 - bl2) % 2, bh2)
}

fn main() {
    input! {
        n: usize,
        xs: [i64; 3],
        ss: [String; n],
    }
    let mut hist = vec![];
    dfs(&ss, 0, xs[0], xs[1], xs[2], &mut hist);
    println!("No");
    return;

    let sum = xs[0] + xs[1] + xs[2];
    let mut memo = vec![];
    {
        let mut ts = vec![];
        for &x in &xs {
            ts.push((x, x));
        }
        memo.push(ts);
    }
    for (i, s) in ss.iter().enumerate() {
        let (al, ah) = memo[i][0];
        let (bl, bh) = memo[i][1];
        let (cl, ch) = memo[i][2];
        if s == "AB" {
            let (al, ah, bl, bh) = step(al, ah, bl, bh);
            //let (al,bh) = if al == 0 { (al,bh) } else { (al-1,bh+1) };
            //let (ah,bl) = if bl == 0 { (ah,bl) } else { (ah+1,bl-1) };
            memo.push(vec![(al, ah), (bl, bh), (cl, ch)]);
        }
        if s == "BC" {
            let (bl, bh, cl, ch) = step(bl, bh, cl, ch);
            //let (bl,ch) = if bl == 0 { (bl,ch) } else { (bl-1,ch+1) };
            //let (bh,cl) = if cl == 0 { (bh,cl) } else { (bh+1,cl-1) };
            memo.push(vec![(al, ah), (bl, bh), (cl, ch)]);
        }
        if s == "AC" {
            let (al, ah, cl, ch) = step(al, ah, cl, ch);
            //let (al,ch) = if al == 0 { (al,ch) } else { (al-1,ch+1) };
            //let (ah,cl) = if cl == 0 { (ah,cl) } else { (ah+1,cl-1) };
            memo.push(vec![(al, ah), (bl, bh), (cl, ch)]);
        }
    }
    for i in 0..n + 1 {
        // eprintln!("memo[{}] = {:?}", i, memo[i]);
    }
    let (mut a, mut b, mut c) = {
        let (al, ah) = memo[n][0];
        let (bl, bh) = memo[n][1];
        let (cl, ch) = memo[n][2];
        if al + bl + cl <= sum && sum <= ah + bh + ch {
            let (mut a2, mut b2, mut c2) = (al, bl, cl);
            let mut rem = sum - al - bl - cl;
            let t = min(rem, ah - al) / 2 * 2;
            a2 += t;
            rem -= t;
            let t = min(rem, bh - bl) / 2 * 2;
            b2 += t;
            rem -= t;
            let t = min(rem, ch - cl) / 2 * 2;
            c2 += t;
            rem -= t;
            if rem != 0 {
                println!("No");
                return;
            }
            (a2, b2, c2)
        } else {
            println!("No");
            return;
        }
    };
    // eprintln!("a = {}, b = {}, c = {}", a, b, c);
    let mut res = vec![];
    for i in (0..n).rev() {
        let (al, ah) = memo[i][0];
        let (bl, bh) = memo[i][1];
        let (cl, ch) = memo[i][2];
        if ss[i] == "AB" {
            if al <= a - 1
                && a - 1 <= ah
                && bl <= b + 1
                && b + 1 <= bh
                && (a + 1 - al) % 2 == 0
                && (b + 1 - bl) % 2 == 0
            {
                res.push("A");
                a -= 1;
                b += 1;
            } else {
                res.push("B");
                a += 1;
                b -= 1;
            }
        }
        if ss[i] == "BC" {
            if bl <= b - 1
                && b - 1 <= bh
                && cl <= c + 1
                && c + 1 <= ch
                && (b + 1 - bl) % 2 == 0
                && (c + 1 - cl) % 2 == 0
            {
                res.push("B");
                b -= 1;
                c += 1;
            } else {
                res.push("C");
                b += 1;
                c -= 1;
            }
        }
        if ss[i] == "AC" {
            if al <= a - 1
                && a - 1 <= ah
                && cl <= c + 1
                && c + 1 <= ch
                && (a + 1 - al) % 2 == 0
                && (c + 1 - cl) % 2 == 0
            {
                res.push("A");
                a -= 1;
                c += 1;
            } else {
                res.push("C");
                a += 1;
                c -= 1;
            }
        }
    }
    println!("Yes");
    for r in res.iter().rev() {
        println!("{}", r);
    }
}