Commits

daclouds committed 74cdb1c

[Qualification Round] Double Squares, Studious Student

Comments (0)

Files changed (6)

resources/DoubleSquares/input.txt

+5
+10
+25
+3
+0
+1

resources/DoubleSquares/output.txt

+1
+2
+0
+1
+1

resources/StudiousStudent/input.txt

+5
+6 facebook hacker cup for studious students
+5 k duz q rc lvraw
+5 mybea zdr yubx xe dyroiy
+5 jibw ji jp bw jibw
+5 uiuy hopji li j dcyi

resources/StudiousStudent/output.txt

+cupfacebookforhackerstudentsstudious
+duzklvrawqrc
+dyroiymybeaxeyubxzdr
+bwjibwjibwjijp
+dcyihopjijliuiuy

src/qualification/DoubleSquares.java

+package qualification;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Scanner;
+
+public class DoubleSquares {
+
+	/**
+	 * @param args
+	 */
+	public static void main(String[] args) {
+		DoubleSquares ds = new DoubleSquares();
+
+		FileReader inputFile = null;
+		FileWriter outputFile = null;
+
+		try {
+			inputFile = new FileReader("resources/DoubleSquares/input.txt");
+			outputFile = new FileWriter("resources/DoubleSquares/output.txt");
+			Scanner scanner = new Scanner(inputFile);
+			int numberOfCases = scanner.nextInt();
+			for (int i = 0; i < numberOfCases; ++i) {
+				int eval = ds.evaluate(scanner.nextInt());
+				outputFile.write(eval + "\n");
+				System.out.println(eval);
+			}
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+			System.exit(1);
+		} catch (IOException e) {
+			e.printStackTrace();
+			System.exit(1);
+		} finally {
+			if (inputFile != null) {
+				try {
+					inputFile.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+
+			if (outputFile != null) {
+				try {
+					outputFile.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+	}
+
+	public int evaluate(int input) {
+		if (input == 0)
+			return 1;
+		
+		HashSet<HashSet<Integer>> result = new HashSet<HashSet<Integer>>();
+		
+		for (int availableMaximumInt = availableMaximumInt(input), i = availableMaximumInt; i > 0; --i) {
+			availableMaximumInt = i;
+			HashSet<Integer> list = new HashSet<Integer>();
+			
+			for(int rest = input; rest > 0; rest -= square(availableMaximumInt), availableMaximumInt = availableMaximumInt(rest)) {
+				if ( !(list.contains(availableMaximumInt)) && (availableMaximumInt != 0)) {
+					list.add(availableMaximumInt);
+				}
+			}
+			
+			if (sumOfSquare(list) == input)
+				result.add(list);
+		}
+		return result.size();
+	}
+
+	public int availableMaximumInt(int input) {
+		int sqrt = 1;
+		if (input == 1)
+			return 1;
+		else
+			sqrt = (int) Math.sqrt(input);
+		return sqrt;
+	}
+
+	public int square(int input) {
+		return input * input;
+	}
+
+	public int sumOfSquare(HashSet<Integer> list) {
+		Iterator<Integer> iter = list.iterator();
+		int sum = 0;
+		while (iter.hasNext()) {
+			sum += square(iter.next());
+		}
+		return sum;
+	}
+}

src/qualification/StudiousStudent.java

+package qualification;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Scanner;
+
+
+public class StudiousStudent {
+	
+	public static void main(String[] args) {
+		StudiousStudent ss = new StudiousStudent();
+
+		FileReader inputFile = null;
+		FileWriter outputFile = null;
+
+		try {
+			inputFile = new FileReader("resources/StudiousStudent/input.txt");
+			outputFile = new FileWriter("resources/StudiousStudent/output.txt");
+			Scanner scanner = new Scanner(inputFile);
+			int numberOfCases = scanner.nextInt();
+			for (int i = 0; i < numberOfCases; ++i) {
+				String line = null;
+				do {
+					line = scanner.nextLine();
+				} while(line.length() <= 0);
+				
+				String eval = ss.evaluate(line);
+				outputFile.write(eval + "\n");
+				System.out.println(eval);
+			}
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+			System.exit(1);
+		} catch (IOException e) {
+			e.printStackTrace();
+			System.exit(1);
+		} finally {
+			if (inputFile != null) {
+				try {
+					inputFile.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+
+			if (outputFile != null) {
+				try {
+					outputFile.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+	}
+
+	private String evaluate(String line) {
+		String[] words = line.split(" ");
+		ArrayList<String> arr = new ArrayList<String>();
+		int length = Integer.parseInt(words[0]);
+		for(int i=1; i<words.length; ++i) {
+			arr.add(words[i]);
+		}
+		Collections.sort(arr, new WordComparator());
+		
+		StringBuffer sb = new StringBuffer();
+		for(String word : arr) {
+			sb.append(word);
+		}
+		return sb.toString();
+	}
+}
+
+class WordComparator implements Comparator<String> {
+
+	@Override
+	public int compare(String string, String anotherString) {
+		if(string.startsWith(anotherString) || anotherString.startsWith(string)) return anotherString.compareTo(string);
+		else return string.compareTo(anotherString); 
+	}
+	
+}