Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Spring 2015 -- CSC 2700 Section 01

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

week02/m/WordAmalgamation3.java

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class WordAmalgamation3
{
    public static String dictionary[];
    public static String mirrorDictionary[];
    public static String scrambled[];
    public static String alphaScrambled[];
    public static String inputString = "";

    public static void main(String args[])
    {
        try
        {
            Scanner keyb = new Scanner(System.in);
            int xCounter = 0;
            String keybLine = keyb.nextLine();
            
            while(xCounter < 2)
            {
                if(keybLine.length() > 6 || keybLine.length() < 1)
                {
                    //Input error
                    return;
                }
                inputString += keybLine + '\n';
                if(keybLine.equals("XXXXXX"))
                {
                    xCounter++;
                    if(xCounter == 2)
                        break;
                }
                keybLine = keyb.nextLine();

            }
            
            
            //Create 2 buffered readers for the inputstring. One counts the inputs to initalize the dictionary and scrambled arrays, the other reads them.
            InputStream input = new ByteArrayInputStream(inputString.getBytes());
            InputStream inputCounter = new ByteArrayInputStream(inputString.getBytes());
            BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));
            BufferedReader inputReaderCounter = new BufferedReader(new InputStreamReader(inputCounter));
            
            //Count each of the inputs to initalize the arrays
            String inputReaderCounterLine = inputReaderCounter.readLine();
            int count =0;
            while(!inputReaderCounterLine.equals("XXXXXX"))
            {
                count++;
                inputReaderCounterLine = inputReaderCounter.readLine();
            }
            dictionary = new String[count];
            count = 0;
            
            inputReaderCounterLine = inputReaderCounter.readLine();
            while(!inputReaderCounterLine.equals("XXXXXX"))
            {
                count++;
                inputReaderCounterLine = inputReaderCounter.readLine();
            }
            scrambled = new String[count];
            
            //Now add each of the dictionary input to the dictionary and the same for the scrambled words
            String inputReaderLine = inputReader.readLine();
            count = 0;
            while(!inputReaderLine.equals("XXXXXX"))
            {
                dictionary[count] = inputReaderLine;
                count++;
                inputReaderLine = inputReader.readLine();
            }
            
            //Sort the dictionary so the output will be alphasorted.
            Arrays.sort(dictionary);
            
            count=0;
            inputReaderLine = inputReader.readLine();   //move past the XXXXXX line
            while(!inputReaderLine.equals("XXXXXX"))
            {
                scrambled[count] = inputReaderLine;
                inputReaderLine = inputReader.readLine();
                count++;
            }

            //populate the mirrordictionary with an alphabetized dictionary
            mirrorDictionary = new String[dictionary.length];
            for (int j = 0; j < dictionary.length; j++)
            {
                mirrorDictionary[j] = alphaScramble(dictionary[j]);
            }

            //populate the alphaScrambled with alphaScrambled words. Sheer poetry right there
            alphaScrambled = new String[scrambled.length];
            for (int j = 0; j < scrambled.length; j++)
            {
                alphaScrambled[j] = alphaScramble(scrambled[j]);
            }


            //compare the scrambled words with the mirrordictionary and output the results
            boolean didOutputSomething = false;
            for (int j = 0; j < scrambled.length; j++)
            {
                for (int k = 0; k < mirrorDictionary.length; k++)
                {
                    if (alphaScrambled[j].equals(mirrorDictionary[k]))
                    {
                        System.out.println(dictionary[k]);
                        didOutputSomething = true;
                    }
                }
                
                if (!didOutputSomething)
                {
                    System.out.println("NOT A VALID WORD");
                }

                System.out.println("******");
                didOutputSomething = false;
            }

        }
        catch (Exception e)
        {
            System.out.println("some error");
            e.printStackTrace();
        }
        
        System.exit(0);
    }

    public static String alphaScramble(String s)
    {
        String answer = "";
        char[] c = s.toCharArray();
        Arrays.sort(c);
        answer = String.copyValueOf(c);
        return answer;
    }

}

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, 2015