// DONATED BY BRIAN NIXON FALL 2016 CS 401  THANKS BRIAN!
import java.io.*;
import java.util.*;

public class Potus
{
	public static void main(String args[]) throws Exception
	{
		TreeMap<String, TreeSet<String>> map = new TreeMap<String, TreeSet<String>>();
		TreeMap<String, String> inverseMap = new TreeMap<String, String>();
		TreeSet<String> stateLess = new TreeSet<String>();
		TreeSet<String> presLess = new TreeSet<String>();
		BufferedReader mapFile = new BufferedReader( new FileReader("state2presidents.txt"));
		
		//Start Map While
		while (mapFile.ready())
		{
			String line = mapFile.readLine();
			String[] tokens = line.split( " " );	//if it's tyler,taylor,nixon	.split(",");	.split("/");	//the array will always be full, no length - 1
			String state = tokens[0];
			TreeSet<String> presidents = new TreeSet<String>();
			for(int i = 1; i <tokens.length ; ++i)
			{
				presidents.add(tokens[i]);
				inverseMap.put(tokens[i], state);
			}
			map.put(state, presidents);
		}
		mapFile.close();
		//End Map While
		
		mapFile = new BufferedReader( new FileReader("allPresidents.txt"));
		
		//Start Presidents While
		while (mapFile.ready())
		{
			String line = mapFile.readLine();
			if(!inverseMap.containsKey(line))
				stateLess.add(line);
		}
		//End Presidents While
		mapFile.close();
		
		mapFile = new BufferedReader( new FileReader("allStates.txt"));
		
		//Starts States While
		while(mapFile.ready())
		{
			String line = mapFile.readLine();
			if(!map.containsKey(line))
				presLess.add(line);
		}
		//End States While
		mapFile.close();
		

		//Print the map
		System.out.println("The following states had these presidents born in them: \n");
		for(String state: map.keySet())
		{
			System.out.print(state + " ");
			for(String president : map.get(state))	//prdouces a treeset	//returns the presidents names in sorted order
				System.out.print(president + " ");
			System.out.println("");	
		}
		System.out.println("\nList of presidents and the state each was born in: \n");
		for(String invPres: inverseMap.keySet())
		{
			System.out.print(invPres + " ");
			System.out.print(inverseMap.get(invPres) + " ");
			System.out.println("");
		}
		System.out.println("\nThese presidents were born before the states were formed: \n");
		for(String noState: stateLess)
			System.out.println(noState);
		System.out.println("\nThese states had no presidents were born in them: \n");
		for(String noPres: presLess)
			System.out.println(noPres);
	}
}
		
 