import java.io.*;
import java.util.*;

public class AnagramMap
{
	private Map map = new HashMap();
	
	public AnagramMap(BufferedReader file) throws IOException
	{
		String s;
		
		while ((s = file.readLine()) != null)
		{
			Word w = new Word(s);
			String sorted = w.getSorted();
			if (map.containsKey(sorted))
			{
				ArrayList alist = (ArrayList)map.get(sorted);
				alist.add(w.getWord());
			}
			else
			{
				ArrayList alist = new ArrayList();
				alist.add(w.getWord());
				map.put(sorted,alist);
			}
		}
		file.close();
		System.out.println("Size = " + map.size());
	}
	
	
	public void generateAnagrams() throws IOException
	{
		String searchKey;
		Word searchWord;
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	  	do
	  	{
	  		System.out.print("\nletters to search [#] to stop: ");			
			searchKey = input.readLine();
			searchWord = new Word(searchKey);
			if (map.containsKey(searchWord.getSorted()))
	  		{
	  			System.out.println(map.get(searchWord.getSorted()));
	  		}
		}
		while (!searchWord.equals("#"));
	}
	

	public static void main(String[] args) throws IOException
	{
		AnagramMap a = new AnagramMap(new BufferedReader(new FileReader("words.txt")));
		a.generateAnagrams();
	}
}
