import java.util.ArrayList;
/**
* A class to find the longest common substrings in two Strings.  Written to prove to Louise how easy this challenge was ;)
* http://www.worthlesscheese.com
* @author Brian
*/
public class CommonSubstrings {
	
	public static void main(String[] args) {
		if (args.length < 2) {
			System.out.println("Not enough arguments supplied - exiting");
			System.exit(0);
		} else {
			
			boolean verbose = false;
			
			if (args.length == 3) { //Which one is the flag?
				
				String[] newargs = new String[2];
				int i = 0;
				
				for (String s : args) {
					if (s.equals("-v")) {
						verbose = true;
					} else {
						if (i > 1) { //Check that we're not outside the array...
							System.out.println("What the heck are you doing?");
							System.out.println("Please supply two Strings and an optional '-v' flag for verbose output");
							System.out.println("Exiting");
							System.exit(0);
						}
						
						newargs[i] = s;
						i++;
						
					}
				}
				
				findLargestSubstring(newargs[0], newargs[1], verbose);
				
			} else if (args.length > 3) { //Too many args
			
				System.out.println("Too many arguments supplied!");
				System.out.println("Please supply two Strings and an optional '-v' flag for verbose output");
				System.out.println("Exiting");
				System.exit(0);
			
			} else { //got to be args[0] and args[1]
				findLargestSubstring(args[0], args[1], false);
			}
		}
	}
	
	private static void findLargestSubstring(String first, String second, boolean verbose) {
		
		ArrayList<String> substr = new ArrayList<String>();
		
		//Firstly we get all the possible substrings from String first.
		for (int i = 0; i < first.length(); i++) { //start char
			for (int j = i+1; j <= first.length(); j++) { //end char
				
				substr.add(first.substring(i,j));
				
			} //Have all substring starting at index i
		} //Have all possible substrings in String first
		
		ArrayList<String> inBoth = new ArrayList<String>();
		
		//Now we pull out the ones that are also in String second.
		for (String s : substr) {
			if (second.contains(s)) {
				inBoth.add(s);
			}
		}
		
		int length = 0;
		
		//If requested, we print out the list of all common substrings.
		//We're actually going around the loop to find the longest substrings, but we might as well do two things at once.
		
		if (verbose) {
			System.out.println("All common substrings:");
		}
		
		for (String s : inBoth) {
			if (verbose) {
				System.out.println(s);
			}
			if (s.length() > length) { //We also find the longest Strings in inBoth
				length = s.length();
			}
		}
		
		if (verbose) {
			System.out.println("\n");
		}
		
		//Finally, we print out all of the String of length `length'
		//Doing it this way accounts for multiple substrings of the same length.
		//We could also have used a for loop instead of a for-each above, and stored the inBoth indices of the matching substrings
		//in an ArrayList, and wiped it each time we found a longer substring, but I prefer this way of doing it.
		//So there.
		
		if (length == 0) {
			System.out.println("There are no common substrings");
			return;
		}
		
		
		System.out.println("Longest substrings are of length " + length + ".  They are:");
		
		for (String s : inBoth) {
			if (s.length() == length) {
				System.out.println("	" + s);
			}
		}
	} //End of method.  That was easy, wasn't it?
	
	
}
