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: Wrong Answer)

#![allow(unused_imports, unused_macros)]

use kyoproio::*;
use std::{
    collections::*,
    io::{self, prelude::*},
    iter,
    mem::{replace, swap},
};

fn main() -> io::Result<()> {
    std::thread::Builder::new()
        .stack_size(64 * 1024 * 1024)
        .spawn(solve)?
        .join()
        .unwrap();
    Ok(())
}

fn solve() {
    let stdin = io::stdin();
    let mut kin = KInput::new(stdin.lock());
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    macro_rules! output { ($($args:expr),+) => { write!(&mut out, $($args),+).unwrap(); }; }
    macro_rules! outputln {
        ($($args:expr),+) => { output!($($args),+); outputln!(); };
        () => { output!("\n"); if cfg!(debug_assertions) { out.flush().unwrap(); } }
    }

    let (n, a, b, c): (usize, usize, usize, usize) = kin.input();
    let mut a = a.min(3);
    let mut b = b.min(3);
    let mut c = c.min(3);
    if a + b + c > 6 {
        a = a.saturating_sub(1);
        b = b.saturating_sub(1);
        c = c.saturating_sub(1);
    }
    let mut dp = vec![[[[false; 4]; 4]; 4]; n + 1];
    dp[0][a][b][c] = true;
    for i in 1..=n {
        let s = kin.str();
        for a in 0..=3 {
            for b in 0..=3 {
                for c in 0..=3 {
                    if !dp[i - 1][a][b][c] {
                        continue;
                    }
                    dbg!(a, b, c);
                    match s {
                        "AB" => {
                            if a > 0 && b < 3 {
                                dp[i][a - 1][b + 1][c] = true;
                            }
                            if b > 0 && a < 3 {
                                dp[i][a + 1][b - 1][c] = true;
                            }
                        },
                        "AC" => {
                            if a > 0 && c < 3 {
                                dp[i][a - 1][b][c + 1] = true;
                            }
                            if c > 0 && a < 3 {
                                dp[i][a + 1][b][c - 1] = true;
                            }
                        },
                        "BC" => {
                            if b > 0 && c < 3 {
                                dp[i][a][b - 1][c + 1] = true;
                            }
                            if c > 0 && b < 3 {
                                dp[i][a][b + 1][c - 1] = true;
                            }
                        },
                        _ => {}
                    }
                }
            }
        }
    }
    let mut a = 0;
    let mut b = 0;
    let mut c = 0;
    'l: for aa in 0..3 {
        for bb in 0..3 {
            for cc in 0..3 {
                if dp[n][aa][bb][cc] {
                    a = aa;
                    b = bb;
                    c = cc;
                    break 'l;
                }
            }
        }
    }
    if a == 0 && b == 0 && c == 0 {
        outputln!("No");
        return;
    }
    let mut res = Vec::new();
    'r: for i in (1..=n).rev() {
        for da in -1..=1 {
            for db in -1..=1 {
                for dc in -1..=1 {
                    if da + db + dc != 0 || (da == 0 && db == 0 && dc == 0) {
                        continue;
                    }
                    let ta = (a as isize - da) as usize;
                    let tb = (b as isize - db) as usize;
                    let tc = (c as isize - dc) as usize;
                    if ta <= 3 && tb <= 3 && tc <= 3 && dp[i - 1][ta][tb][tc] {
                        res.push(if da == 1 {
                            'A'
                        } else if db == 1 {
                            'B'
                        } else if dc == 1 {
                            'C'
                        } else {
                            panic!();
                        });
                        a = ta;
                        b = tb;
                        c = tc;
                        continue 'r;
                    }
                }
            }
        }
    }
    outputln!("Yes");
    for d in res.into_iter().rev() {
        outputln!("{}", d);
    }
}


// -----------------------------------------------------------------------------
pub mod kyoproio {
    use std::io::prelude::*;
    pub trait Input {
        fn str(&mut self) -> &str;
        fn input<T: InputParse>(&mut self) -> T {
            T::input(self)
        }
        fn iter<T: InputParse>(&mut self) -> Iter<T, Self> {
            Iter(self, std::marker::PhantomData)
        }
        fn seq<T: InputParse, B: std::iter::FromIterator<T>>(&mut self, n: usize) -> B {
            self.iter().take(n).collect()
        }
    }
    pub struct KInput<R> {
        src: R,
        buf: String,
        pos: usize,
    }
    impl<R: BufRead> KInput<R> {
        pub fn new(src: R) -> Self {
            Self {
                src,
                buf: String::with_capacity(1024),
                pos: 0,
            }
        }
    }
    impl<R: BufRead> Input for KInput<R> {
        fn str(&mut self) -> &str {
            loop {
                if self.pos >= self.buf.len() {
                    self.pos = 0;
                    self.buf.clear();
                    if self.src.read_line(&mut self.buf).expect("io error") == 0 {
                        return &self.buf;
                    }
                }
                let range = self.pos
                    ..self.buf[self.pos..]
                        .find(|c: char| c.is_ascii_whitespace())
                        .map(|i| i + self.pos)
                        .unwrap_or_else(|| self.buf.len());
                self.pos = range.end + 1;
                if range.end > range.start {
                    return &self.buf[range];
                }
            }
        }
    }
    pub struct Iter<'a, T, I: ?Sized>(&'a mut I, std::marker::PhantomData<*const T>);
    impl<'a, T: InputParse, I: Input + ?Sized> Iterator for Iter<'a, T, I> {
        type Item = T;
        fn next(&mut self) -> Option<T> {
            Some(self.0.input())
        }
    }
    pub trait InputParse: Sized {
        fn input<I: Input + ?Sized>(src: &mut I) -> Self;
    }
    impl InputParse for Vec<u8> {
        fn input<I: Input + ?Sized>(src: &mut I) -> Self {
            src.str().as_bytes().to_owned()
        }
    }
    macro_rules! from_str_impl {
        { $($T:ty)* } => {
            $(impl InputParse for $T {
                fn input<I: Input + ?Sized>(src: &mut I) -> Self {
                    src.str().parse::<$T>().expect("parse error")
                }
            })*
        }
    }
    from_str_impl! {
        String char bool f32 f64 isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128
    }
    macro_rules! tuple_impl {
        ($H:ident $($T:ident)*) => {
            impl<$H: InputParse, $($T: InputParse),*> InputParse for ($H, $($T),*) {
                fn input<I: Input + ?Sized>(src: &mut I) -> Self {
                    ($H::input(src), $($T::input(src)),*)
                }
            }
            tuple_impl!($($T)*);
        };
        () => {}
    }
    tuple_impl!(A B C D E F G);
}