UVA 489 – Hangman Judge

Pretty easy problem. The word to guess can be safely put into a HashSet and we can keep guessing until the set is empty or until we are done with (7) guesses that are allowed. One thing to note is that the order of the input matters but duplicates in the input do not matter. Therefore, I use an ArrayList as well as a HashSet to store the input and get AC.

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P489 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    while (sc.hasNextInt()) {
      int N = sc.nextInt();
      if (N == -1)
        break;

      char[] word = sc.next().toCharArray();
      HashSet<Character> hax = new HashSet<Character>();
      for (Character c : word) {
        hax.add(c);
      }

      // Order Matters
      char[] guesses = sc.next().toCharArray();
      ArrayList<Character> hax3 = new ArrayList<Character>();
      HashSet<Character> hax2 = new HashSet<Character>();
      for (Character c : guesses) {
        if (!hax2.contains(c)) {
          hax3.add(c);
        }
        hax2.add(c);
      }

      int movesLeft = 7;
      for (Character c : hax3) {
        if (hax.contains(c)) {
          hax.remove(c);
        } else {
          movesLeft--;
        }
        if (hax.size() == 0 || movesLeft == 0)
          break;
      }

      out.println("Round " + N);
      if (movesLeft < 1) {
        out.println("You lose.");
      } else if (hax.size() == 0) {
        out.println("You win.");
      } else {
        out.println("You chickened out.");
      }
    }

    out.close();
    sc.close();
  }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.