/*
	F12 cs401 Instructor Tim Hoffman
	FINAL EXAM PROGRAMMING PROBLEM

	YOUR NAME: Virginia Mulky
	YOUR PITT ID: vjm10
*/


import java.util.*;
import java.io.*;

public class PotusSolution2
{
	public static void main( String[] args )  throws Exception
	{
		BufferedReader infile1 = new BufferedReader( new FileReader("state2Presidents.txt") );
		BufferedReader infile2 = new BufferedReader( new FileReader("allPresidents.txt") );
		BufferedReader infile3 = new BufferedReader( new FileReader("allStates.txt") );

		// YOUR CODE HERE TO DECLARE MAPS OR SETS TO HOLD THE DATA OF THESE THREE INPUT FILES
		TreeMap<String, TreeSet<String>> state2Presidents= new TreeMap<String, TreeSet<String>>();
		TreeSet<String> presidents= new TreeSet<String>();
		TreeSet<String> allPresidents= new TreeSet<String>();
		TreeSet<String> allStates= new TreeSet<String>();

		// ################# STEP #1 worth 60% or 70% ###################

		// YOUR CODE HERE TO READ infile1 INTO A Map

		while (infile1.ready())
		{
			ArrayList<String> s2P= new ArrayList<String>(Arrays.asList(infile1.readLine().split(" ")));
			String state= s2P.get(0); s2P.remove(0);
			state2Presidents.put(state, new TreeSet<String>(s2P));

		}

		System.out.println( "The following states had these presidents born in them:\n");  // DO NOT REMOVE OR MODIFY

		for (String state : state2Presidents.keySet())
		{
			System.out.print(state + " ");

			for (String name : state2Presidents.get(state))
				System.out.print(name + " ");

			System.out.println();

		}

		// YOUR CODE HERE TO *PRINT* THE MAP ACCORDING TO THe FORMAT SPECIFIED FOR STEP #1
		// DUMP IT LAZY = 60%  OR  PRINT IT FORMATTED = 70%

		// ################# STEP #2 worth 15% ###################

		System.out.println( "\nList of presidents and the state each was born in:\n");  // DO NOT REMOVE OR MODIFY

		// YOUR CODE HERE TO PRINT THE INVERSION OF THE MAP worth 15%
		for (String state : state2Presidents.keySet())
		{
			presidents.addAll(state2Presidents.get(state));

		}



		for(String name : presidents)
		{
			for (String state : state2Presidents.keySet())
			{
				if (state2Presidents.get(state).contains(name))
					System.out.print(name + " " + state);
			}
				System.out.println();
		}





		// ################# STEP #3 worth 10% ###################

		System.out.println( "\nThese presidents were born before the states were formed:\n");  // DO NOT REMOVE OR MODIFY

		// YOUR CODE HERE TO PRINT THE NAME(S) Of ANY PRESIDENT(s)
		//  WHO WERE BORN BEFORE THE STATES WERE FORMED = 10%

		while (infile2.ready())
		{
			allPresidents.add(infile2.readLine());
		}

		TreeSet<String> noState= new TreeSet<String>(allPresidents);

		for (String name : allPresidents)
		{
			for (String state : state2Presidents.keySet())
			{
				if (state2Presidents.get(state).contains(name))
					noState.remove(name);

			}
		}

		for (String name : noState)
			System.out.println(name);




		// ################# STEP #4 worth 5% ###################

		System.out.println( "\nThese states had no presidents were born in them:\n");

		// YOUR CODE HERE TO PRINT THE NAME(S) OF ANY STATE(s) WHICH HAD NO PRESIDENT BORN IN THEM 5%

		while (infile3.ready())
		{
			allStates.add(infile3.readLine());
		}

		for (String state : allStates)
		{
			if (!state2Presidents.keySet().contains(state))
				System.out.println(state);
		}


	} // END MAIN

	//              - - - - - - - - - - -  H E L P E R    M E T H O D S     D O W N    H E R E  - - - - - - - - - -







}	// END POTUS CLASS