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_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    input!{
        n: usize,
        a: usize,
        b: usize,
        c: usize,
        v: [chars; n]
    }
    let mut dp = vec![vec![vec![vec![(0, 0, 0, 'S', false); 3]; 3]; 3]; n + 1];
    let ai = if a >= 2 {2} else {a};
    let bi = if b >= 2 {2} else {b};
    let ci = if c >= 2 {2} else {c};
    dp[0][ai][bi][ci].4 = true;
    for i in 0..n {
        if v[i] == vec!['A', 'B'] {
            for ai in 0..3 {
                for bi in 1..3 {
                    for ci in 0..3 {
                        if !dp[i][ai][bi][ci].4 {
                            continue;
                        }
                        if ai < 2 {
                            if dp[i + 1][ai + 1][bi - 1][ci].4 {
                                continue;
                            }
                            dp[i + 1][ai + 1][bi - 1][ci] = (ai, bi, ci, 'A', true);
                        } else {
                            if dp[i + 1][ai][bi - 1][ci].4 {
                                continue;
                            }
                            dp[i + 1][ai][bi - 1][ci] = (ai, bi, ci, 'A', true);
                        }
                    }
                }
            }
            for ai in 1..3 {
                for bi in 0..3 {
                    for ci in 0..3 {
                        if !dp[i][ai][bi][ci].4 {
                            continue;
                        }
                        if bi < 2 {
                            if dp[i + 1][ai - 1][bi + 1][ci].4 {
                                continue;
                            }
                            dp[i + 1][ai - 1][bi + 1][ci] = (ai, bi, ci, 'B', true);
                        } else {
                            if dp[i + 1][ai - 1][bi][ci].4 {
                                continue;
                            }
                            dp[i + 1][ai - 1][bi][ci] = (ai, bi, ci, 'B', true);
                        }
                    }
                }
            }
        } else if v[i] == vec!['A', 'C'] {
            for ai in 0..3 {
                for bi in 0..3 {
                    for ci in 1..3 {
                        if !dp[i][ai][bi][ci].4 {
                            continue;
                        }
                        if ai < 2 {
                            if dp[i + 1][ai + 1][bi][ci - 1].4 {
                                continue;
                            }
                            dp[i + 1][ai + 1][bi][ci - 1] = (ai, bi, ci, 'A', true);
                        } else {
                            if dp[i + 1][ai][bi][ci - 1].4 {
                                continue;
                            }
                            dp[i + 1][ai][bi][ci - 1] = (ai, bi, ci, 'A', true);
                        }
                    }
                }
            }
            for ai in 1..3 {
                for bi in 0..3 {
                    for ci in 0..3 {
                        if !dp[i][ai][bi][ci].4 {
                            continue;
                        }
                        if ci < 2 {
                            if dp[i + 1][ai - 1][bi][ci + 1].4 {
                                continue;
                            }
                            dp[i + 1][ai - 1][bi][ci + 1] = (ai, bi, ci, 'C', true);
                        } else {
                            if dp[i + 1][ai - 1][bi][ci].4 {
                                continue;
                            }
                            dp[i + 1][ai - 1][bi][ci] = (ai, bi, ci, 'C', true);
                        }
                    }
                }
            }
        } else {
            for ai in 0..3 {
                for bi in 0..3 {
                    for ci in 1..3 {
                        if !dp[i][ai][bi][ci].4 {
                            continue;
                        }
                        if bi < 2 {
                            if dp[i + 1][ai][bi + 1][ci - 1].4 {
                                continue;
                            }
                            dp[i + 1][ai][bi + 1][ci - 1] = (ai, bi, ci, 'B', true);
                        } else {
                            if dp[i + 1][ai][bi][ci - 1].4 {
                                continue;
                            }
                            dp[i + 1][ai][bi][ci - 1] = (ai, bi, ci, 'B', true);
                        }
                    }
                }
            }
            for ai in 0..3 {
                for bi in 1..3 {
                    for ci in 0..3 {
                        if !dp[i][ai][bi][ci].4 {
                            continue;
                        }
                        if ci < 2 {
                            if dp[i + 1][ai][bi - 1][ci + 1].4 {
                                continue;
                            }
                            dp[i + 1][ai][bi - 1][ci + 1] = (ai, bi, ci, 'C', true);
                        } else {
                            if dp[i + 1][ai][bi - 1][ci].4 {
                                continue;
                            }
                            dp[i + 1][ai][bi - 1][ci] = (ai, bi, ci, 'C', true);
                        }
                    }
                }
            }
        }
    }
    for i in 0..3 {
        for j in 0..3 {
            for k in 0..3 {
                if !dp[n][i][j][k].4 {
                    continue;
                }
                println!("Yes");
                let mut ai = i;
                let mut bi = j;
                let mut ci = k;
                let mut path = vec![];
                for i in (1..=n).rev() {
                    path.push(dp[i][ai][bi][ci].3);
                    let t = dp[i][ai][bi][ci];
                    ai = t.0;
                    bi = t.1;
                    ci = t.2;
                }
                for &c in path.iter().rev() {
                    println!("{}", c);
                }
                return;
            }
        }
    }
    println!("No");
}