import java.io.*; import java.util.*; import java.awt.*; class nowin_judge { public Scanner sc; public PrintStream ps; public String toString() { return "nowin_judge"; } // Keep track of a hand. // This will work for both the player and the dealer public class Hand { // Keep track of two things - the value (points) of this hand, // and the number of aces which are being used as 11 (instead of 1) public int value, aces; public Hand() { value = aces = 0; } public Hand( Hand h ) { value = h.value; aces = h.aces; } public void deal( char card ) { switch( card ) { // Start off with the assumption that all aces are worth 11. // We'll knock'em down to 1 if we have to - that is, if the // hand's score is over 21 case 'A' : value += 11; ++aces; break; case '2' : value += 2; break; case '3' : value += 3; break; case '4' : value += 4; break; case '5' : value += 5; break; case '6' : value += 6; break; case '7' : value += 7; break; case '8' : value += 8; break; case '9' : value += 9; break; case 'T' : // All of these cards case 'J' : // (T,J,Q,K) case 'Q' : // are worth 10 case 'K' : value += 10; break; default : System.out.println( "PANIC!! Don't know this card! " + card ); } // While the score is >21 and we have aces being used as 11, knock'em down to 1. // An Ace going from 11 to 1 decreases the score by 10. while( value>21 && aces>0 ) { value -= 10; aces--; } } }; public void doit() throws Exception { sc = new Scanner( System.in ); ps = System.out; for(;;) { char deck[] = sc.next().toUpperCase().toCharArray(); // If it's the end of input, the line is "JOKER". // The first letter is 'J', which could also be a Jack // - but the second letter is 'O'. That's unique. if( deck[1]=='O' ) break; boolean win = false; Hand player = new Hand(); Hand savedealer = new Hand(); // Deal out the first four cards - player, dealer, player, dealer. player.deal( deck[0] ); savedealer.deal( deck[1] ); player.deal( deck[2] ); savedealer.deal( deck[3] ); // The next card is the 5th in the deck (4 with 0-based arrays) int nextcard = 4; // Keep going until the player wins (or busts) while( !win ) { // Player busts if( player.value>21 ) break; // Go back to the dealer's original hand Hand dealer = new Hand( savedealer ); // Deal cards to the dealer until the dealer's hand is worth more than 17 int card = nextcard; while( dealer.value<17 ) { dealer.deal( deck[card++] ); } // Check for a win if( player.value>=dealer.value || dealer.value>21 ) { win = true; } else { // If not a win, reset. // Deal the next card to the player and go back to the top. player.deal( deck[nextcard++] ); } } ps.println( win ? "Yes" : "No" ); } } public static void main(String[] args) throws Exception { new nowin_judge().doit(); } }