Esempio n. 1
0
  public static void main(String[] args) throws FileNotFoundException {
    System.out.println("Welcome to the CSE 143 anagram solver.");
    System.out.println("Using dictionary file " + DICTIONARY_FILE + ".");

    // read dictionary into a set
    Scanner input = new Scanner(new File(DICTIONARY_FILE));
    Set<String> dictionary = new TreeSet<String>();
    while (input.hasNextLine()) {
      dictionary.add(input.nextLine());
    }
    dictionary = Collections.unmodifiableSet(dictionary); // read-only

    // create Anagrams object for, well, solving anagrams
    Anagrams solver = new Anagrams(dictionary);

    // get first phrase to solve
    Scanner console = new Scanner(System.in);
    String phrase = getPhrase(console);

    // loop to get/solve each phrase
    while (phrase.length() > 0) {
      System.out.println("All words found in \"" + phrase + "\":");
      Set<String> allWords = solver.getWords(phrase);
      System.out.println(allWords);
      System.out.println();

      System.out.print("Max words to include (Enter for no max)? ");
      String line = console.nextLine().trim();

      long startTime = System.currentTimeMillis();
      if (line.length() > 0) {
        // use a max
        int max = new Scanner(line).nextInt();
        solver.print(phrase, max); // print all anagrams of phrase
      } else {
        // no max
        solver.print(phrase); // print all anagrams of phrase
      }
      long endTime = System.currentTimeMillis();
      System.out.println();

      // 12247 ms elapsed, 2594392 unique LetterInventory object(s) created
      if (TIMING) {
        long elapsed = endTime - startTime;
        int inventories = LetterInventory.getInstanceCount();
        System.out.println(
            elapsed + " ms elapsed, " + inventories + " unique LetterInventory object(s) created");
        LetterInventory.resetInstanceCount();
      }

      // get next phrase to solve
      phrase = getPhrase(console);
    }
  }