Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Fall 2014 -- CSC 2700 Section 01

[Isaac's Home Page ]  [Mailing List ]  [Class Page ]  [Normal ]  

week04/b/Main.java



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.HashMap;
import java.util.TreeSet;
import java.util.SortedSet;
import java.util.Scanner;

/**
 *
 * @author ared38
 */
public class Main {

    HashMap<Long, SortedSet<String>> dict = new HashMap();
    Scanner cin;

    static long lPow(long x, long pow) {
        long result = x;
        for (long i = 1; i < pow; i++) {
            result *= x;
        }
        return result;
    }

    void store(String word) {
        SortedSet<String> bucket;
        bucket = dict.get(encode(word));
        if(bucket==null){
            bucket=new TreeSet();
            dict.put(encode(word), bucket);
        }
        bucket.add(word);
    }

    void fetch(String word) {
        try {
            SortedSet<String> bucket = dict.get(encode(word));
            for (String s : bucket) {
                System.out.println(s);
            }
        } catch (Exception e) {
            System.out.println("NOT A VALID WORD");
        }
    }

    Long encode(String word) {
        long result = 0;
        for (char c : word.toCharArray()) {
            result += lPow(2, (long) (c - 'a'));
        }
        return result;
    }

    void getDict() {
        while (true) {
            String s = cin.next();
            if (s.charAt(0) == 'X') {
                break;
            }
            store(s);
        }
    }

    void getScramble() {
        while (true) {
            String s = cin.next();
            if (s.charAt(0) == 'X') {
                break;
            }
            fetch(s);
            System.out.println("******");
        }
    }

    public void doIt() {
        // TODO code application logic here
        cin = new Scanner(System.in);
        getDict();
        getScramble();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        (new Main()).doIt();
    }
}

The statements and opinions included in these pages are those of only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014